![](/img/trans.png)
[英]Use closure instead selector argument for UIBarButtonItem BUT without using weak self
[英]Swift use selector argument like a closure
我只是想知道是否可以将一个函数传递给一个按钮动作(通常是一个选择器)。
例如,通常我会说:
UIBarButtonItem(title: "Press", style: .Done, target: self, action: "functionToCall")
func functionToCall() {
// Do something
}
但我想知道是否可以做以下事情:
UIBarButtonItem(title: "Press", style: .Done, target: self, action: {
// Do Something
})
我想要这样做的原因是因为我的功能非常简单,看起来它更整洁,更像Swift,因为它们强调它们放在封闭上。
这是Swift 3的更新解决方案。
class BlockBarButtonItem: UIBarButtonItem {
private var actionHandler: ((Void) -> Void)?
convenience init(title: String?, style: UIBarButtonItemStyle, actionHandler: ((Void) -> 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) -> Void)?) {
self.init(image: image, style: style, target: nil, action: #selector(barButtonItemPressed))
self.target = self
self.actionHandler = actionHandler
}
func barButtonItemPressed(sender: UIBarButtonItem) {
actionHandler?()
}
}
这是一个没有子类化的替代解决方案:
extension UIBarButtonItem {
private struct AssociatedObject {
static var key = "action_closure_key"
}
var actionClosure: (()->Void)? {
get {
return objc_getAssociatedObject(self, &AssociatedObject.key) as? ()->Void
}
set {
objc_setAssociatedObject(self, &AssociatedObject.key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
target = self
action = #selector(didTapButton(sender:))
}
}
@objc func didTapButton(sender: Any) {
actionClosure?()
}
}
它依赖于Objective-C运行时的关联对象来添加闭包/块属性。
设置时,它将目标更改为自身,并将选择器指向一个新函数,如果存在,则调用该闭包。
这种情况,在任何时刻,你可以只设置actionClosure
任何UIBarButtonItem
,并期望一切工作,这里有一个例子:
let button = UIBarButtonItem(
barButtonSystemItem: .action,
target: nil, action: nil
)
button.actionClosure = {
print("hello")
}
Reddit上的一篇文章用自定义组件解释了这个问题 - https://www.reddit.com/r/swift/comments/3fjzap/creating_button_action_programatically_using
要使用它,虽然我必须以编程方式而不是通过故事板添加Button。 我是这样做的。
let tempVariableIWantToReference = "Classy"
navigationTitle.leftBarButtonItem = BlockBarButtonItem.init(
title: "< Back", style: UIBarButtonItemStyle.Plain,
actionHandler: { () -> Void in
print("Hey I'm in a closure")
print("I can even reference temporary variables. \(self.tempVariableIWantToReference)!!!")
})
typealias
的函数签名清理语法。 UIBarButtonItem
传递给动作处理程序以符合Cocoa约定。 import UIKit
class SwiftBarButtonItem: UIBarButtonItem {
typealias ActionHandler = (UIBarButtonItem) -> Void
private var actionHandler: ActionHandler?
convenience init(image: UIImage?, style: UIBarButtonItem.Style, actionHandler: ActionHandler?) {
self.init(image: image, style: style, target: nil, action: #selector(barButtonItemPressed(sender:)))
target = self
self.actionHandler = actionHandler
}
convenience init(title: String?, style: UIBarButtonItem.Style, actionHandler: ActionHandler?) {
self.init(title: title, style: style, target: nil, action: #selector(barButtonItemPressed(sender:)))
target = self
self.actionHandler = actionHandler
}
convenience init(barButtonSystemItem systemItem: UIBarButtonItem.SystemItem, actionHandler: ActionHandler?) {
self.init(barButtonSystemItem: systemItem, target: nil, action: #selector(barButtonItemPressed(sender:)))
target = self
self.actionHandler = actionHandler
}
@objc func barButtonItemPressed(sender: UIBarButtonItem) {
actionHandler?(sender)
}
}
不幸的是,Apple提供的初始化程序是不可能的。 它在后台运行的方式是使用反射,并且提供闭包是完全不同的,目前不支持。
您可能能够通过一些黑客攻击来创建自定义解决方案,或者Apple可能会在将来引入该解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.