I have a LazyVStack presenting Cells. My main View which has the LazyVStack has a viewModel.
@StateObject private var model = ViewModel()
The Stack presents a list of CustomViews.
LazyVStack {
ForEach(model.items) { item in
CustomView(item: item)
}
}
Each custom view consists from two other subviews, CustomView1 and CustomView2. CustomView2 has a button, where onTap I want to flip the cell.
However at that point my Custom views have a new copy of these items (struct). What is the best approach to point back to the model to achieve:
.onTapGesture {
model.flipItem(item)
}
- Somehow multiple @Binding variables is an option ?
- Making the ViewModel shared singleton for easy access ?
- Better option ?
I would add a property inside your
Item
struct that stores if the card is flipped or not:Then loop over
model.items.indices
, so that you can get theBinding
:And your
CustomView
/CustomView1
will need to look something like this.