1

I have a List of cells. Each cell includes a button, which, when tapped, shows a popover.

Button(action: { showingPopover = true },
       label:  { Image(systemName: "gearshape") })
   .buttonStyle(BorderlessButtonStyle())
   .popover(isPresented:   $showingPopover,
            arrowEdge:     .leading) {
                    AccessoryIconView(viewModel: viewModel.iconViewModel())
                        .frame( width: 200, height: 300)
            }

The problem is that, when the popover is shown, and I tap the button of another cell, the current popover is not dismissing and the new popover does not show, with this message in the console :

Attempt to present <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x10460cab0> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x104006f60> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVVS_22_VariadicView_Children7ElementGVS_18StyleContextWriterVS_19SidebarStyleContext___: 0x104218480>) 
which is already presenting <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x104409b30>.

How can I tell SwiftUI to first properly dismiss the shown popover before showing the new one ?

AirXygène
  • 2,409
  • 15
  • 34
  • How about using `showingPopover =!showingPopover` instead of setting it to true always?That way it will get toggled at each click no? – gtxtreme Sep 03 '21 at 05:46
  • Tried it, but that's not the solution. The problem is when tapping another cell's button - so anyway the current popover's cell showingPopover will remain to true – AirXygène Sep 03 '21 at 05:51
  • 2
    Use instead `.popover(item:` modifier and move it out of List, like in https://stackoverflow.com/questions/66290017/first-popover-showing-outdated-state – Asperi Sep 03 '21 at 06:02
  • do you use the same `showingPopover` variable for all your buttons, or are they different, as they should be. – workingdog support Ukraine Sep 03 '21 at 06:53
  • have you had a look at this answer: https://stackoverflow.com/questions/56517400/swiftui-dismiss-modal to dismiss a modal view and reset the `showingPopover` – workingdog support Ukraine Sep 03 '21 at 06:59
  • Thanks for your suggestions, but none of them is really solving the problem. After having played a lot, I can conclude that there is no solution. swiftUI does not allow that a button dismisses a popover and then shows up another, unless there is a higher level of controlling that takes care of that. – AirXygène Sep 03 '21 at 17:36
  • I think concluding that there is no solution to this is premature. Show us an example code that reproduces your problem, and let the SO community have go at solving it. At the moment all we have is a few lines of code without context. This is not enough, all we can do is suggest options. We need a simple example that we can run and observe your issues. – workingdog support Ukraine Sep 04 '21 at 00:28

0 Answers0