首页 > 解决方案 > SwiftUI - 如何使用 TabView 弹出到根目录?

问题描述

在 NavigationView 中使用 TabView 时,我无法弹出到根目录工作。交换这些顺序不符合我的应用程序的设计。

我已按照 此答案中的解决方案进行操作,但无济于事:

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 相关的行,则 pop to root 有效。

标签: swiftuiswiftui-navigationlinkswiftui-navigationview

解决方案


推荐阅读