How to make onTapGesture and ForEach work with each other?

I'm trying out working on a reminder app with SwiftUI. My ContentView has a list of reminders in a ForEach, which opens each reminder in a ReminderView. The main ContentView has a state variable isModal which shows a modal sheet when trying to edit a particular reminder, so I have the .onTapGesture in the ForEach, instead of inside each ReminderView struct.

However, it appears that whenever we tap any reminder, it just thinks we tapped the first one. Any way to fix this?

ForEach(model.reminders, id: \.self) { reminder in
                ReminderView(reminder: reminder)
                    .onTapGesture {
                        self.isEModal = true
                    }
                    .sheet(isPresented: $isEModal, content: {
                        EditView(model: self, reminder: reminder)
                    })
                    .frame(height: geometry.size.height * 0.08)
            }

Are you sharing a single isEModal amongst all the models? If so then the code is trying to display the sheet for every reminder when the isEModal is toggled. You will need a isModal for each reminder, or maybe have a currentSelection and create a boolean binding based on that.

Ah I see. I'll try that out, thanks!

Terms of Service

Privacy Policy

Cookie Policy