【问题标题】:SwiftUI: How to switch to a new navigation stack with NavigationViewsSwiftUI:如何使用 NavigationViews 切换到新的导航堆栈
【发布时间】:2023-04-04 03:15:01
【问题描述】:

我目前正在使用 SwiftUI Beta 5。我有一个工作流程,其中涉及在一系列视图中导航。最后一个视图涉及将大量数据填充到应用程序中并结束该特定工作流的操作。

下载数据后,用户应该能够开始新的工作流程。我想“忘记”旧的 NavigationView,因为一旦工作流完成,返回导航堆栈是没有用的。相反,我想导航到“启动”视图,该视图有效地成为新导航视图的根。

如何使用 SwiftUI NavigagationViews 使用导航堆栈中的一个视图导航到具有不同 NavigationView 的另一个视图(从而成为新导航堆栈的根)?

【问题讨论】:

  • 你有没有看到这个stackoverflow.com/a/57513566/7786555 另外,当我过去尝试在 beta 5 中这样做时,没有简单的方法。既然我们有 beta 6,我需要重新审视它。
  • @kontiki,谢谢,是的,我看到了。直接移回根目录是一种选择。 (发帖人自己承认,如果堆栈中有很多视图,它可能会有点笨拙。)我有兴趣“横向”导航到新的根视图,而不是回到原来的根视图。

标签: swift swiftui navigationview ios-navigationview


【解决方案1】:

首先,对不起,我想发表一个简单的评论,但没有足够的声誉点:(

我刚刚更新了我在stackoverflow.com/a/57513566/7786555 回到你的根目录的方式

实际上,您的评论给了我关于回到根目录的新方法的想法。拥有一个新的根视图。如果您强制刷新管理根视图的结构视图,那么它将自动执行您想要的操作。下面只是回到根目录(没有动画)。您可以调整示例以更改根视图(而不是使用相同的视图)以满足您的需要。

struct DetailViewB: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    @State var fullDissmiss:Bool = false
    var body: some View {
        SGNavigationChildsView(fullDissmiss: self.fullDissmiss){
            VStack {
                Text("This is Detail View B.")

                Button(action: { self.presentationMode.wrappedValue.dismiss() } )
                { Text("Pop to Detail View A.") }

                Button(action: {
                    self.fullDissmiss = true
                } )
                { Text("Pop two levels to Master View with SGGoToRoot.") }
            }
        }
    }
}

struct DetailViewA: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

    @State var fullDissmiss:Bool = false
    var body: some View {
        SGNavigationChildsView(fullDissmiss: self.fullDissmiss){
            VStack {
                Text("This is Detail View A.")

                NavigationLink(destination: DetailViewB() )
                { Text("Push to Detail View B.") }

                Button(action: { self.presentationMode.wrappedValue.dismiss() } )
                { Text("Pop one level to Master.") }

                Button(action: { self.fullDissmiss = true } )
                { Text("Pop one level to Master with SGGoToRoot.") }
            }
        }
    }
}

struct MasterView: View {
    var body: some View {
        VStack {
            Text("This is Master View.")
            NavigationLink(destination: DetailViewA() )
            { Text("Push to Detail View A.") }
        }
    }
}

struct ContentView: View {

    var body: some View {
        SGRootNavigationView{
            MasterView()
        }
    }
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

struct SGRootNavigationView<Content>: View where Content: View {
    let cancellable = NotificationCenter.default.publisher(for: Notification.Name("SGGoToRoot"), object: nil)

    let content: () -> Content

    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }

    @State var goToRoot:Bool = false

    var body: some View {
        return
            Group{
            if goToRoot == false{
                NavigationView {
                content()
                }
            }else{
                NavigationView {
                content()
                }
            }
            }.onReceive(cancellable, perform: {_ in
                DispatchQueue.main.async {
                    self.goToRoot.toggle()
                }
            })
    }
}

struct SGNavigationChildsView<Content>: View where Content: View {
    let notification = Notification(name: Notification.Name("SGGoToRoot"))

    var fullDissmiss:Bool{
        get{ return false }
        set{ if newValue {self.goToRoot()} }
    }

    let content: () -> Content

    init(fullDissmiss:Bool, @ViewBuilder content: @escaping () -> Content) {
        self.content = content
        self.fullDissmiss = fullDissmiss
    }

    var body: some View {
        return Group{
            content()
        }
    }

    func goToRoot(){
        NotificationCenter.default.post(self.notification)
    }
}

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多