SwiftUI - 如何使用 TabView 弹出到 Root?

时间:2021-03-13 01:11:22

标签: swiftui swiftui-navigationlink swiftui-navigationview

在 NavigationView 中使用 TabView 时,我无法让 pop 到 root 工作。交换这些顺序不适合我的应用程序的设计。

我遵循了解决方案 this answer 但无济于事:

struct ContentView: View {
    @State var selection = 0
    var body: some View {
        NavigationView {
            TabView(selection: $selection) {
                FirstTabView()
                    .tabItem {
                        Label("Home", systemImage: "house")
                    }
                    .tag(0)
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct FirstTabView: View {
    @State var isActive = false
    var body: some View {
        NavigationLink("SecondView Link", destination: SecondView(secondViewIsActive: $isActive), isActive: $isActive)
            .isDetailLink(false)
    }
}

struct SecondView: View {
    @Binding var secondViewIsActive: Bool
    var body: some View {
        NavigationLink("ThirdView Link", destination: ThirdView(thirdViewIsActive: $secondViewIsActive))
    }
}

struct ThirdView: View {
    @Binding var thirdViewIsActive: Bool
    var body: some View {
        Text("Third View")
        Button("Pop To Root") {
            thirdViewIsActive = false
        }
    }
}

调试控制台显示此错误:

Trying to pop to a missing destination at /Library/Caches/com.apple.xbs/Sources/Monoceros_Sim/Monoceros-120/Shared/NavigationBridge_PhoneTV.swift:341

如果我注释掉上面与 TabView 相关的行,则弹出到 root 即可。

0 个答案:

没有答案