导航上的编程导航视图深度超过两个视图



有没有办法使用 NavigationView 对两个以上的视图进行编程导航?

像这样:View 1 -> View 2 -> View 3

这是我正在尝试执行的示例代码:

class Coordinator: ObservableObject {
@Published var selectedTag: String?
}
struct ContentView: View {
@EnvironmentObject var coordinator: Coordinator
let things = ["first", "second", "third"]
var body: some View {
NavigationView {
List(things, id: .self) { thing in
NavigationLink(destination: SecondView(thing: thing),
tag: thing,
selection: self.$coordinator.selectedTag) {
Text(thing)
}
}
}
}
}
struct SecondView: View {
@EnvironmentObject var coordinator: Coordinator
let thing: String
let things = ["fourth", "fifth", "sixth"]
var body: some View {
List(things, id: .self) { thing2 in
NavigationLink(destination: ThirdView(thing: self.thing, thing2: thing2),
tag: thing2,
selection: self.$coordinator.selectedTag) {
Text(thing2)
}
}
}
}
struct ThirdView: View {
let thing: String
let thing2: String
var body: some View {
Text("(thing) (thing2)")
}
}

我希望我可以选择一个特定的标签并深入导航到 ThirdView,但即使是简单的导航也不起作用。如果我在 SecondView 上选择一个链接,它将向前导航,然后向后导航,而不是像预期的那样向前导航。

我还尝试使用 2 个变量,一个来表示每个屏幕的标签,但它也不起作用。

有没有办法做到这一点?我做错了什么吗?

目前,您的代码就像从SecondViewThirdView有另一个导航流,我假设您不打算这样做。如果是这样的话,你也应该把SecondView的身体包裹成一个NavigationView

警告:您将有 2 个导航栏。

如果没有tagselection(SecondViewContentView(,您的代码可以按预期工作。在这里使用tagselection是否有特定原因?我找不到一个合适的解决方案,既使用它们又只有一个NavigationView.

最新更新