3

I'm trying to present multiple modals on top of my home view, but when I try to dismiss all modals, there is only the first one that close... (I know, there is a lot of subject about this, but I didn't found any solution that was working for me...)

Any ideas?

Here is my testing code:

struct ContentView: View {
    @State var presentA = false
    
    var body: some View {
        Button("Present A") { presentA = true }
            .sheet(isPresented: $presentA) { ContentViewA(presentAll: $presentA) }
    }
}

struct ContentViewA: View {
    @Binding var presentAll: Bool
    @State var presentB = false

    var body: some View {
        Button("Present B") { presentB = true }
            .sheet(isPresented: $presentB) { ContentViewB(presentAll: $presentAll) }
    }
}

struct ContentViewB: View {
    @Binding var presentAll: Bool

    var body: some View {
        Button("Close all") {
            presentAll = false
        }
    }
}

So when I touch the "Close all" button, I go back to the ContentViewA instead of the ContentView...

In my memory this was working with the previous version of SwiftUI but it seems that's not working anymore...

What am I doing wrong?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

1 Answers1

1

I don't think it is a valid user flow and SwiftUI does not handle it. The possible workaround is the same as for UIKit

Tested with Xcode 14b3 / iOS 16

var body: some View {
    Button("Close all") {
        UIApplication.shared.keyWindow?
          .rootViewController?
          .dismiss(animated: false, completion: nil) // false is important !!
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690