Learn Courses My Dashboard

Popover in Loop

Hello,

is there anyone who can tell me how to use a popover in a loop with the isPresented variable?
Currently I have something like this:

ForEach(0…<30, id: .self) { index in

                    Button {
                        
                        presentPopover = true
                        
                    } label: {
                        
                        ZStack {
                            
                            Rectangle()
                                .fill(.white)
                                .frame(width: 180, height: 55)
                                .cornerRadius(5)
                            
                            Text("Runde \(index + 1)")
                                .bold()
                                .font(.system(size: 24))
                                .foregroundColor(.black)
                        }
                        
                    }
                    .popover(isPresented: $presentPopover) {
                        GameSheetPopOverView(points: $points)
                    }
                    
                    
                }

But here nothing happens when I tap on the button. This works only if I have single elements and a isPresented variable for each element.

Thanks in advance.

Instead of having a Button inside your loop, add an .onTapGesture modifier to the closing brace of your ZStack which will set presentPopover = true

Thank you Chris_Parker I solved it with an extra sub view. So the complete stuff in my ForEach is now in a sub view with the index as a parameter.