Im trying to pop back to a specific view point or the root view with navigationDestination(isPresented) being used to push views.
Here is a simpler version of the code I am working with
import SwiftUI
struct View1: View {
@State var goToView2 = false
@State var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
VStack {
Text("View 1")
Button("Go to View 2") {
goToView2 = true
}
}.navigationDestination(isPresented: $goToView2) {
View2(path: $path)
}
}
}
}
struct View2: View {
@State var goToView3 = false
@Binding var path: NavigationPath
var body: some View {
VStack {
Text("View 2")
Button("Go to View 3") {
goToView3 = true
}
}.navigationDestination(isPresented: $goToView3) {
View3(path: $path)
}
}
}
struct View3: View {
@Binding var path: NavigationPath
var body: some View {
VStack {
Text("View 3")
Button("Go to View 1") {
print("Before: \(path.count)")
path = .init()
print("After: \(path.count)")
}
}
}
}
I don't exactly know what else to try. I've tried appending to the path value on change, but as expected, that does not work. Also, the path count is set to 0. Any help is appreciated
Here is the result of the previous code:
navigationDestination(isPresented, destination)
initializerisPresented
initializer.