1
votes

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
You may want to keep your variables in the Environment instead of passing them in the NavigationLink parameters. That way you won’t run in to problems when you navigate back to the first View and pop the second view off the Navigation stack.Ryan

1 Answers

2
votes

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