无法使用 NavigationView 和 NavigationLink 在 SwiftUI 中弹出当前屏幕

Sp1*_*ire 5 ios swift swiftui swiftui-navigationlink

因此,我在尝试弹出当前屏幕时遇到了有关SwiftUI中的NavigationViewNavigationLinks的问题,这在本例中不起作用。

我有 3 个屏幕 A -> B -> C

每个屏幕都有一个用于弹出自身的按钮。

从 B -> A 的弹出有效,但是当您从 A -> B -> C 导航时,从 C -> B 的弹出不起作用。

你知道会出现什么问题吗?我将附上下面的代码和视频。该应用程序应支持iOS 14

在此输入图像描述

struct ScreenA: View {
    @State private var showB = false
    
    var body: some View {
        NavigationView {
            VStack {
                Button("Go to B") {
                    showB = true
                }
                
                NavigationLink(destination: ScreenB(didTapGoBack: {
                    showB = false
                }), isActive: $showB) {
                    EmptyView()
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
struct ScreenB: View {
    var didTapGoBack: () -> Void
    @State var showC = false

    var body: some View {
        VStack {
            Button("Go to C") {
                showC = true
            }
        
            Button("Back to Screen A") {
                didTapGoBack()
            }
            
            NavigationLink(destination: ScreenC(didTapGoBack: {
                showC = false
            }), isActive: $showC) {
                EmptyView()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
struct ScreenC: View {
    var didTapGoBack: () -> Void
    
    var body: some View {
        VStack {
            Text("Screen C")
            Button("Back to Screen B") {
                didTapGoBack()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
@main
struct TestNavApp: App {
    var body: some Scene {
        WindowGroup {
            ScreenA()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Sp1*_*ire 1

.isDetailLink(false)因此,解决此问题的解决方案是在第一个上添加NavigationLink,也许NavigationView默认情况下会像主从一样进行拆分导航,所以也许这就是第一个弹出窗口起作用的原因,但是通过添加此内容,您将告诉 NavigationView 作为堆栈工作。

现在导航按预期工作 A -> B -> C:

struct ScreenA: View {
    @State private var showB = false
    
    var body: some View {
        NavigationView {
            VStack {
                Button("Go to B") {
                    showB = true
                }
                
                NavigationLink(destination: ScreenB(didTapGoBack: {
                    showB = false
                }), isActive: $showB) {
                    EmptyView()
                }.isDetailLink(false)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)