SwiftUI Navigation through multiple screens

后端 未结 1 1805
忘掉有多难
忘掉有多难 2021-01-25 17:33

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 NavigationVie

1条回答
  •  情歌与酒
    2021-01-25 17:56

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

    0 讨论(0)
提交回复
热议问题