navigationDestination(isPresented) 与 swiftui 4 中的路径

Eme*_*own 5 swiftui swiftui-navigationstack

我试图弹回到特定的视点或根视图,并使用 navigationDestination(isPresented) 来推送视图。

这是我正在使用的代码的更简单版本

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)")
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

我完全不知道还能尝试什么。我尝试在更改时附加到路径值,但正如预期的那样,这不起作用。另外,路径计数设置为 0。感谢任何帮助

这是前面代码的结果:

在此输入图像描述