this is the code
struct ContentView: View {
@State var path = NavigationPath()
var body: some View {
NavigationStack(path: $path){
List{
NavigationLink {
View1(){
print(path.count)
print(path)
}
} label: {
Text("level0 ")
}
}
}
}
}
struct View1: View {
var go: () -> Void
var body: some View {
List{
NavigationLink {
View2(){
go()
print("went to view2")
}
} label: {
Text("level 1 ")
}
}
.onAppear(){
print("view1 enter")
go()
}
}
}
struct View2: View {
var go: ()-> Void
var body: some View {
List{
NavigationLink {
View3(){
go()
print("went to view3")
}
} label: {
Text("level 2 ")
}
}
.onAppear(){
print("view2 enter")
go()
}
}
}
struct View3: View {
var go: ()-> Void
var body: some View {
List{
Text("level 3")
.onAppear(){
print("view3 enter")
go()
}
}
}
}
and this is the print log
view1 enter
0
NavigationPath(_items: SwiftUI.NavigationPath.(unknown context at $1c5c2c248).Representation.eager([]), subsequentItems: [], iterationIndex: 0)
view2 enter
0
NavigationPath(_items: SwiftUI.NavigationPath.(unknown context at $1c5c2c248).Representation.eager([]), subsequentItems: [], iterationIndex: 0)
went to view2
view3 enter
0
NavigationPath(_items: SwiftUI.NavigationPath.(unknown context at $1c5c2c248).Representation.eager([]), subsequentItems: [], iterationIndex: 0)
went to view2
went to view3
question
NavigationPath not updating with NavigationStack,
when I open view1 and view2 ,why NavigationPath not update?
1
1
1.5k