繁体   English   中英

可以从托管的 SwiftUI 视图调用 UIKit UIViewController 中的闭包吗?

[英]Is it ok to call closures in UIKit UIViewController from Hosted SwiftUI View?

我在WrapperViewController托管ContentView ,如下所示:

class WrapperViewController: UIViewController {
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        
        self.view = UIView()

        var contentView = ContentView()
        contentView.buttonTapped = { [weak self] in
            self?.tappedButton()
        }
        
        let contentViewController = UIHostingController(rootView: contentView)
        
        self.addChild(contentViewController)
        self.view.addSubview(contentViewController.view)

        contentViewController.view.frame = view.bounds
        contentViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        contentViewController.didMove(toParent: self)
    }
    
    func tappedButton() {
        print("tapped button")
    }
}

struct ContentView: View {
    var buttonTapped: (() -> Void)?
    
    var body: some View {
        Button("Tap me") {
            buttonTapped?()
        }
    }
}

我正在从ContentView调用buttonTapped中的WrapperViewController ,但这可以吗? “点击按钮”正在打印,但我读了这篇文章

当您在结构中使用闭包时,闭包表现为引用类型,问题由此开始。 闭包需要引用外部环境,以便在执行闭包主体时可以修改环境。

 struct Car { var speed: Float = 0.0 var increaseSpeed: (() -> ())? } var myCar = Car() myCar.increaseSpeed = { myCar.speed += 30 // The retain cycle occurs here. We cannot use [weak myCar] as myCar is a value type. }

那么我应该在这里使用闭包吗? 还是我应该做点别的?

我们不能使用 [weak myCar],因为 myCar 是一个值类型。

在文章的示例中, myCar是一个struct

但是,在您的代码中:

contentView.buttonTapped = { [weak self] in
    self?.tappedButton()
}

self是一个UIViewController ,它是一个(和一个引用类型)。

您对[weak self]的使用非常好:闭包将保持对 UIViewController 的弱引用,当您点击按钮时,将tappedButton()函数。 这可能是您所期望的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM