SwiftUI - SwiftUI 中是否有等效的 popViewController? [英] SwiftUI - Is there a popViewController equivalent in SwiftUI?

查看:36
本文介绍了SwiftUI - SwiftUI 中是否有等效的 popViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 SwiftUI 并希望能够在点击按钮时返回上一个视图,就像我们在 UINavigationController 中使用 popViewController 一样.到目前为止有没有提供的方法来做到这一点?

I was playing around with SwiftUI and want to be able to come back to the previous view when tapping a button, the same we use popViewController inside a UINavigationController. Is there a provided way to do it so far ?

我也尝试使用 NavigationDestinationLink 来实现,但没有成功.

I've also tried to use NavigationDestinationLink to do so without success.

struct AView: View {
    var body: some View {
        NavigationView {
            NavigationButton(destination: BView()) {
                Text("Go to B")
            }
        }
    }
}

struct BView: View {
    var body: some View {
        Button(action: {
            // Trying to go back to the previous view
            // previously: navigationController.popViewController(animated: true)
        }) {
            Text("Come back to A")
        }
    }
}

推荐答案

按如下方式修改您的 BView 结构.该按钮的功能与 UIKit 中的 popViewController 一样.

Modify your BView struct as follows. The button will perform just as popViewController did in UIKit.

struct BView: View {
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>
    var body: some View {
        Button(action: { self.mode.wrappedValue.dismiss() })
        { Text("Come back to A") }
    }
}

这篇关于SwiftUI - SwiftUI 中是否有等效的 popViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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