本文介绍了关闭UIHostingController中包含的SwiftUI视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将登录视图控制器重写为SwiftUI View . SignInView 被包装在 UIHostingController 子类中(最终类SignInViewController:UIHostingController< SignInView> {} ),并以模态形式全屏显示,当需要登录时.

I have rewritten my sign in view controller as a SwiftUI View. The SignInView is wrapped in a UIHostingController subclass (final class SignInViewController: UIHostingController<SignInView> {}), and is presented modally, full screen, when sign in is necessary.

一切正常,除了我不知道如何从 SignInView 中关闭 SignInViewController .我尝试添加:

Everything is working fine, except I can't figure out how to dismiss the SignInViewController from the SignInView. I have tried adding:

@Environment(\.isPresented) var isPresented

登录成功后,在 SignInView

并将其分配给 false ,但这似乎并不与UIKit互操作.如何关闭视图?

in SignInView and assigning it to false when sign in is successful, but this doesn't appear to interop with UIKit. How can I dismiss the view?

推荐答案

我最终找到了比提供的解决方案简单得多的解决方案:

I ended up finding a much simpler solution than what was offered:


final class SettingsViewController: UIHostingController<SettingsView> {
    required init?(coder: NSCoder) {
        super.init(coder: coder, rootView: SettingsView())
        rootView.dismiss = dismiss
    }

    func dismiss() {
        dismiss(animated: true, completion: nil)
    }
}

struct SettingsView: View {
    var dismiss: (() -> Void)?

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Button("Dimiss", action: dismiss!)
                }
            }
            .navigationBarTitle("Settings")
        }
    }
}

这篇关于关闭UIHostingController中包含的SwiftUI视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 02:47