SwiftUI Navigation through multiple screens

淺唱寂寞╮ 提交于 2021-02-05 07:49:26

问题


I'm a bit confused on how navigation works in SwiftUI. Does only the view starting the navigation need a NavigationView? I have one view with a NavigationView that has a NavigationLink to a second view. The second view then has a NavigationLink to a third and final view.

However, when my second view navigates to my third view, I get this message in the logs:

unbalanced calls to begin/end appearance transitions for <_TtGC7SwiftUI19UIHostingControllerVS_7AnyView_: 0x7f85d844bf90>.

I don't know if I'm handling navigation through multiple screens correctly and I'm getting some really odd behavior where pressing Next on my second screen takes me back to my first somehow...

//This is the link in my first view, my seconds link is the same except it does to my next step and the tag is different
NavigationLink(
    destination: PasswordView(store: self.store),
    tag: RegisterState.Step.password,
    selection: .constant(store.value.step)
)

回答1:


Navigation is a little bit tricky in SwiftUI, after creating one navigationview you don't need to create again in your 2nd or 3rd view. I am not sure how you creating it firstly. Here is an example how navigation is working.

import SwiftUI

struct ContentView: View {
       var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: SecondView()) {
                    Text("Show Second View")
                }.navigationBarTitle("FirstView", displayMode: .inline)
            }
        }
    }
}

struct SecondView: View {
    var body: some View {
        NavigationLink(destination: ThirdView()) {
            Text("Show Third view")
        }.navigationBarTitle("SecondView", displayMode: .inline)
    }    
}

struct ThirdView: View {
    var body: some View {
        Text("This is third view")
            .navigationBarTitle("ThirdView", displayMode: .inline)
    }
}


来源:https://stackoverflow.com/questions/58600054/swiftui-navigation-through-multiple-screens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!