SwiftUI iOS 16,关闭多个模态视图不起作用。

3
我正在尝试在主视图的顶部呈现多个模态窗口,但是当我尝试关闭所有模态窗口时,只有第一个会关闭... (我知道,这方面有很多主题,但我没有找到适合我的解决方案...)
有什么想法吗?
这是我的测试代码:
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
        }
    }
}

当我点击“全部关闭”按钮时,我回到了ContentViewA而不是ContentView...

在我的记忆中,这在之前的SwiftUI版本中有效,但现在好像不起作用了...

我做错了什么?

1个回答

1
我不认为这是一个有效的用户流程,SwiftUI也无法处理它。可能的解决方法与UIKit相同。
已在Xcode 14b3 / iOS 16上进行了测试。
var body: some View {
    Button("Close all") {
        UIApplication.shared.keyWindow?
          .rootViewController?
          .dismiss(animated: false, completion: nil) // false is important !!
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接