繁体   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()
    }
}

我想要第二个视图 -> 第三个视图
但 swiftUi 行为是:第二视图 -> rootView -> 第三视图

并在 'click go to ThirdView' 中快速点击,'Third View' 出现错误行为。 return to rootView

如何解决这个问题或者我做错了?

下面是一个更简单的版本。

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