如何在 SwiftUI 中放松到 MainView [英] How can I unwind to the MainView in SwiftUI

查看:17
本文介绍了如何在 SwiftUI 中放松到 MainView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不在另一个 NavigationView 顶部显示第二个 NavigationView,我找不到从 ViewB 转到 ContentView 的方法.

I can't find a way to go to the ContentView from ViewB without showing a secound NavigationView on top of the other NavigationView.

struct ContentView: View {
    var body: some View {
        NavigationView{
            VStack {
                Text("Go to ViewA")
                NavigationLink(destination: ViewA()) {Text("Go")}
            }.navigationBarTitle("ContentView")
        }
    }
}

struct ViewA: View {
   @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack {
            Text("Go to ViewB")
            NavigationLink(destination: ViewB()) {Text("Go to B")}
            Text("Go back")
            Button(action: { self.presentationMode.wrappedValue.dismiss() }) {Text("Go to ContentView")}
        }.navigationBarTitle("ViewA")
    }
}

struct ViewB: View {
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack {
            Text("Go Home")
            NavigationLink(destination: ContentView()) {Text("Go")}
            Text("Go back")
            Button(action: { self.presentationMode.wrappedValue.dismiss() }) {Text("Go to ViewB")}
        }.navigationBarTitle("ViewB")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

在旧的"Swift 中,我可以轻松地进行像

In the "old" Swift i could easily make an unwindSegue like

@IBAction func unwindToVCMain(segue:UIStoryboardSegue) { }
}

并轻松调用它

performSegue(withIdentifier: "unwindToVcMain", sender: nil)

推荐答案

使用 Swiftui NavigationLink

首先,将您的第一个视图嵌入到导航控制器中.

Using the Swiftui NavigationLink

First, embed your first view in a navigation controller.

然后插入一个导航链接到下一个视图.NavigationLink 有不同的初始化程序.使用 isActive 参数选择这个:

Then you insert a navigation link to the next view. The NavigationLink has different initialisers. Choose this one with the isActive parameter:

一开始有点费解,但是当点击链接时NavigationLink中的isActive参数变为true导航移动到下一个屏幕.

It is a bit mind-bending at first, but the isActive parameter in the NavigationLink becomes true when the link is clicked and the navigation moves to the next screen.

它创建一个实例,在活动时呈现目的地

It creates an instance that presents a destination when active

如果我传递一个绑定来跟踪这个属性的状态,如果在我随后的视图链中的任何时候再次将它设置为 false,它将重置并返回到原始屏幕!挺整洁的!!

If I pass a binding keeping track of the state of this property, if at any time in my subsequent chain of views I set it to false again, it will reset and return to the original screen! It is quite neat!!

在代码中查看此简短示例:

See this short example in code:

struct ContentView: View {
    @State private var isActive: Bool = false

    var body: some View {
        NavigationView {
            NavigationLink(
                destination: SecondView(isActive: $isActive),
                isActive: $isActive,
                label: {
                    Text("Go to second View")
                })
                .navigationTitle("MainView")
        }
    }
}

我的第二个视图:

struct SecondView: View {
    @Binding var isActive: Bool

    var body: some View {
            NavigationLink("Go to Third View",destination:  ThirdView(isActive: $isActive))
                .navigationTitle("Second View")
    }
}

这里是放松按钮,但它可以在任何视图中.设置 isActive = false 在这里有一些魔力!

And here the button to unwind, but it could be in any view. Setting isActive = false does some magic here!

struct ThirdView: View {
    @Binding var isActive: Bool

    var body: some View {
        Button("Unwind!"){
            isActive = false
        }
        .navigationTitle("Last View!")
    }
}

这篇关于如何在 SwiftUI 中放松到 MainView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆