简体   繁体   中英

NavigationLink repeats in swiftUi

struct Conte111ntView: View {
    @State private var selection: String? = nil
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: Text("Second View : click go to ThirdView ") .navigationBarTitle("Navigation").navigationBarHidden(true).gesture(TapGesture().onEnded{ v in
                    self.selection = "Third"
                }), tag: "Second", selection: $selection) { EmptyView() }.isDetailLink(true)
                NavigationLink(destination: Text("Third View  : click go to SecondView ") .navigationBarTitle("Navigation").navigationBarHidden(true).gesture(TapGesture().onEnded{ v in
                    self.selection = "Second"
                }), tag: "Third", selection: $selection) { EmptyView() }.isDetailLink(true)
                Button("Tap to show second") {
                    self.selection = "Second"
                }
                Button("Tap to show third") {
                    self.selection = "Third"
                }
            }
            .navigationBarTitle("Navigation").navigationBarHidden(true)
        }
    }
}

struct test_Previews: PreviewProvider {
    static var previews: some View {
        Conte111ntView()
    }
}

I want to Second View -> Third View
but swiftUi behavior is: Second View -> rootView -> Third View

And quick tap in 'click go to ThirdView' And ,'Third View' it get the wrong behavior 。 return to rootView

how can fix this Or am I doing it the wrong way?

The following is a simpler version.

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SecondView()) {
                    Text("Second View : click go to ThirdView")
                }
                Spacer()
                NavigationLink(destination: ThirdView()) {
                    Text("Third View  : click go to SecondView")
                }
            }
        }
        .navigationBarHidden(false) 
    }
}

struct SecondView: View {
    var body: some View {
        Text("SecondView is here!")
    }
}

struct ThirdView: View {
    var body: some View {
        Text("ThirdView is here!")
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM