代码之家  ›  专栏  ›  技术社区  ›  scrrr

显示警报后从NavigationLink返回

  •  0
  • scrrr  · 技术社区  · 6 年前

    我正在使用 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.
    

    我做错了什么?

    下面是我如何显示内部警报的 :

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

    showAlert 这是真的。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Simon    6 年前

    @Binding ? SwiftUI pop view

    如果是这样的话,这段代码工作得非常好,没有任何错误

    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")))
                }
            
        }
    }