如何从Viewcontroller中取消SwiftUI?

3

在我的视图控制器中,我有这个函数:

...{
let vc = UIHostingController(rootView: SwiftUIView())
        present(vc, animated: true, completion: nil)
}

如何展示下面的SwiftUIView。

Q 当自定义按钮按下时,如何关闭SwiftUIView?

struct SwiftUIView : View {
     var body: some View {
       CustomButton()
     }   
}

struct CustomButton: View {

    var body: some View {
        Button(action: {
            self.buttonAction()
        }) {
            Text(buttonTitle)
        }
    }

    func buttonAction() {
        //dismiss the SwiftUIView when this button pressed
    }       
}

1
您看过这个问题吗?https://dev59.com/DVMI5IYBdhLWcg3wM4h0 - user7014451
你必须定义一个 @State 并将其传递给相关的控制视图和被控制视图。 - Mojtaba Hosseini
以下内容。如果你还没有尝试过,你可以通过向下拖动控制器来忽略它,但是...我肯定想要并且正在寻找一个在按钮按下时忽略的解决方案。 :) - ovatsug25
@ovatsug25,是的,我需要按钮按下事件,因为当拖动以解除时,.onDisappear回调不会触发,所以需要定义一个操作来执行一些函数。 - RyanTCB
这个回答解决了你的问题吗?如何关闭包含在UIHostingController中的SwiftUI视图 - Curiosity
1个回答

0
struct CustomButton: View {

    var body: some View {
        Button(action: {
            self.buttonAction()
        }) {
            Text(buttonTitle)
        }
    }

    func buttonAction() {
        if let topController = UIApplication.topViewController() {
            topController.dismiss(animated: true)
        }
    }       
}

extension UIApplication {
    class func topViewController(controller: UIViewController? = UIApplication.shared.windows.first { $0.isKeyWindow }?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        return controller
    }
}

或者如果这不起作用,因为您在顶部有一个不同的视图控制器,或者您需要使用视图生命周期事件(onDisappear和onAppear无法与UIHostingController一起使用)。 您可以使用以下方法:

final class SwiftUIViewController: UIHostingController<CustomButton> {
    required init?(coder: NSCoder) {
        super.init(coder: coder, rootView: CustomButton())
        rootView.dismiss = dismiss
    }
    
    init() {
        super.init(rootView: CustomButton())
        rootView.dismiss = dismiss
    }
    
    func dismiss() {
        dismiss(animated: true, completion: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        rootView.prepareExit()
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        rootView.doExit()
    }
}

struct CustomButton: View {
    var dismiss: (() -> Void)?
    
    var body: some View {
        Button(action: dismiss! ) {
            Text("Dismiss")
        }
    }
    func prepareExit() {
        // code to execute on viewWillDisappear
    }
    func doExit() {
        // code to execute on viewDidDisappear
    }
}

对我来说,这个答案出现了以下错误:类型“UIApplication”没有成员“topViewController”。 - Doug
1
@Doug 抱歉,我忘记了我正在使用一个扩展程序,它将关闭顶部的 ViewController。 - Watermamal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接