ios - SwiftUI,从 subview 中关闭模态/页面

标签 ios swiftui dismiss

我是 Swift/SwiftUI 的新手,想知道如何从嵌套的 subview 中关闭模态/页面。

首先,我从 Flutter、UIHostingController 调用,然后是 SwiftUI 页面。 (当前显示为模态...)
导航到 SwiftUI 后,我无法使用 subview 中的@environment 数据。

有什么方法可以让它起作用吗? 提前致谢。

AppDelegate.swift

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        let controller : FlutterViewController = self.window?.rootViewController as! FlutterViewController
        let channel = FlutterMethodChannel.init(name: "com.example.show", binaryMessenger: controller.binaryMessenger)
        channel.setMethodCallHandler({
            (call, result) -> Void in
            if call.method == "sample" {
                let vc = UIHostingController(rootView: ContentView())
                vc.modalPresentationStyle = .fullScreen
                controller.present(vc, animated: true,completion: nil)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ContentView.swift

struct ContentView: View{
    @Environment(\.presentationMode) var presentation: Binding<PresentationMode>
    private var childView: ChildView()

    var body: some View{
        NavigationView{
            ZStack{
                childView
                Button(action: {
// This works ************************
                    self.presentation.wrappedValue.dismiss()
                }, label: {
                    Text("close")
                })
            }
        }
    }
}

ChildView.swift

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

    var body: some View{
        Button(action: {
// This won't do anything *********************************
            self.presentation.wrappedValue.dismiss()
// nor this↓ **********************************************
            if #available(iOS 15.0, *) {
               @Environment(\.dismiss) var dismiss;
                dismiss()
                dismiss.callAsFunction()
            }
            }, label: {
                Text("close")
            })
    }
}

最佳答案

因为您有另一个 NavigationView 是您的 ContentView,所以 ChildView 中的 @Environment(\.presentation) 是一个 subview 而不是父 View 。基本上这两个来自完全不同的导航堆栈。

为了仍然将 NavigationView 保留在您的父 ContentView 中,您需要从 ChildView 的构造函数而不是环境传递表示值:

ContentView.swift

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

    var body: some View{
        NavigationView{
            ZStack{
                ChildView(parentPresentation: presentation)
                Button(action: {
                    self.presentation.wrappedValue.dismiss()
                }, label: {
                    Text("close")
                })
            }
        }
    }
}

在 subview 中,使用普通属性而不是 @Environment

ChildView.swift

struct ChildView: View{
    let parentPresentation: Binding<PresentationMode>

    var body: some View{
        Button(action: {
            self.parentPresentation.wrappedValue.dismiss()
            if #available(iOS 15.0, *) {
                @Environment(\.dismiss) var dismiss;
                dismiss()
                dismiss.callAsFunction()
            }
        }, label: {
            Text("Close")
        })
    }
}

关于ios - SwiftUI,从 subview 中关闭模态/页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70923441/

相关文章:

ios - 代码不解除键盘

iphone - 导航 Controller 不旋转

ios - 更改搜索栏中的字段背景

arrays - SwiftUI 从列表中删除行

android - 单击外部时不要关闭 PopupWindow,仅当单击“关闭”按钮时才关闭

ios - 解除 UIActivityViewController 后的操作

ios - Sprite 套件 | swift 3 |将关卡编辑器中的 SKSpritenode 分配给 SKNode

ios - 如何将 Unmanaged<CFString> 转换为 NSString?

SwiftUI 菜单 - 菜单实际打开时的操作

ios - 如何在SwiftUI分段选择器中更改选定的分段颜色