Hello,
I was wondering if someone could clear-up my thinking here.
e.g. consider the code below...
It has a rootView with a navlink to a childView which in turn has navlinks to GrandchildViews.
The root view uses basic navLInks NavigationLink{View} label: {View}
The child view uses type-based navLinks navigationLink(value:) {View} and .navigationDestination(for:) {View}
I would expect the basic navlinks to work in the root view and the type-based ones to work in the child view. However it appears that both are active when one taps on a link in the child view.
e.g. User actions:
Start -> RootView is only view on the stack -> (tap on ‘Child View’) -> ChildView is top of the stack -> tap on ‘Alice’ -> a second ChildView is top of the stack with a GrandchildView underneath….
Why does this happen, why are the basic links also applied to the childView's links?
Thanks.
struct Thing: Identifiable, Hashable {
let id = UUID()
let name: String
}
struct RootView: View {
var body: some View {
NavigationStack {
List {
NavigationLink {
ChildView()
} label: {
Label("Child View", systemImage: "figure.and.child.holdinghands")
}
NavigationLink {
Text("Hello")
} label: {
Label("Another navLink item in the list", systemImage: "circle")
}
}
.padding()
}
}
}
struct ChildView: View {
private var things = [
Thing(name: "Alice"),
Thing(name: "Bob"),
Thing(name: "Charlie"),
]
var body: some View {
Text("This is the child view")
List {
ForEach(things) { thing in
NavigationLink(value: thing) {
Text(thing.name)
}
}
}
.navigationTitle("Child View")
.navigationDestination(for: Thing.self) { thing in
GrandchildView(thing: thing)
}
}
}
struct GrandchildView: View {
let thing: Thing
var body: some View {
Text("This is the GrandchildView: \(thing.name)")
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI