简体   繁体   English

NavigationLink 在 swiftUi 中重复

[英]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但 swiftUi 行为是:第二视图 -> rootView -> 第三视图

And quick tap in 'click go to ThirdView' And ,'Third View' it get the wrong behavior 。 return to rootView并在 'click go to ThirdView' 中快速点击,'Third View' 出现错误行为。 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!")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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