显示警报后从NavigationLink返回

时间:2020-07-18 20:12:05

标签: swiftui

我正在使用NavigationLink这样“推送”新视图:

NavigationLink(destination: SomeView(shown: $isActive)), isActive: $isActive) { ... }

SomeView内,我现在可以将shown设置为false以“弹出”。这行得通。

但是,如果在SomeView中显示警报,则该警报将不再起作用。显示警报时,我还会在控制台中收到此错误消息:

popToViewController:transition: called on <_TtGC7SwiftUI41StyleContextSplitViewNavigationControllerVS_19SidebarStyleContext_ 0x7ffc1e858c00> while an existing transition or presentation is occurring; the navigation stack will not be updated.

我在做什么错了?

这是我在SomeView内显示警报的方式:

VStack
{
// ...
}
.alert(isPresented:$showAlert)
{
    Alert(title: Text("Some text"), message: Text("More text"), dismissButton: .default(Text("OK")))
}

我通过将showAlert设置为true来显示警报。

1 个答案:

答案 0 :(得分:0)

还是您有/想用@Binding来做到这一点? 还是这种在视图和选项之间导航的方法? SwiftUI pop view

如果是这样,此代码可以正常工作而没有任何错误 在iOS 13.6上测试

struct ContentView: View {
    
    
    var body: some View {
        NavigationView() {
            NavigationLink(destination: SomeView()) {
                Text("Go to child")
            }
        }
    }
}


struct SomeView: View {
    
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @State private var showAlert: Bool = false
    
    var body: some View {
        VStack() {
            
            Text("I'm the child and if you press me an alert is comming up")
                .onTapGesture {
                    self.showAlert.toggle()
                }
            
            Text("Go Back")
                .onTapGesture {
                    self.presentationMode.wrappedValue.dismiss()
                }
            
        }.alert(isPresented: $showAlert)
            {
                Alert(title: Text("Some text"), message: Text("More text"), dismissButton: .default(Text("OK")))
            }
        
    }
}