简体   繁体   English

使用闭包代替UIBarButtonItem BUT的选择器参数,而无需使用弱self

[英]Use closure instead selector argument for UIBarButtonItem BUT without using weak self

In order to use closure in argument of UIBarButtonItem I am using a subclass: 为了在UIBarButtonItem参数中使用闭包,我使用了一个子类:

class ActionBarButtonItem: UIBarButtonItem {
    private var actionHandler: (() -> Void)?

    convenience init(title: String?, style: UIBarButtonItemStyle, actionHandler: (() -> Void)?) {
        self.init(title: title, style: style, target: nil, action: #selector(barButtonItemPressed))
        self.target = self
        self.actionHandler = actionHandler
    }

    convenience init(image: UIImage?, style: UIBarButtonItemStyle, actionHandler: (() -> Void)?) {
        self.init(image: image, style: style, target: nil, action: #selector(barButtonItemPressed))
        self.target = self
        self.actionHandler = actionHandler
    }

    @objc func barButtonItemPressed(sender: UIBarButtonItem) {
        actionHandler?()
    }
}

but now I need to weak [weak self] : 但现在我需要弱点[weak self]

    self.add(barButton: .menu, position: .left) { [weak self] in
        guard let strongSelf = self else {return}
        strongSelf.openMenu()
    }

is there a way to still use closure as selector but not save the closure to avoid using weak self everywhere and you may forget it somewhere ? 有没有办法仍然使用闭包作为选择器,但不保存闭包以避免在任何地方使用弱self,而您可能会在某个地方忘记它?

In a word, no. 一言以蔽之。

You have to save the closure if you're going to call it later. 如果以后要调用它,则必须保存该闭包。 If you're saving a closure, and that closure refers to self , you should make self part of a capture list to avoid a retain cycle. 如果要保存闭包,并且该闭包引用了self ,则应将self设置为捕获列表的一部分,以避免保留周期。 That's what capture lists are for, and is the correct coding pattern for this situation. 这就是捕获列表的用途,并且是这种情况下的正确编码模式。

Any time you refer to self in a closure you need to stop and think about retain cycles. 每当您在闭包中引用self时,您都需要停止并考虑保留周期。

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

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