5

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:

enter image description here

4
  • Does this answer your question? How to send extra data using NavigationStack with SwiftUI? Nov 21, 2022 at 23:05
  • No, I updated the question to match my scenario more. The path is empty right now, whereas his path is not. He also does not use the navigationDestination(isPresented, destination) initializer Nov 21, 2022 at 23:24
  • 1
    There is no way to identify a view with the isPresented initializer. Nov 21, 2022 at 23:31
  • I came across the same bug. I think [ isPresented] modifier can not work with [path of NavigationStack] for now. If you want to use path, you have to use navigationDestination(for data, @ViewBuilder destination) to navigate. But Apple did not mention that in their documents. Maybe Apple will solve this problem in future?
    – Rufus
    Apr 24, 2023 at 3:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.