我正在尝试创建一个具有全局扩展的框架,以使我的工作更简单。现在,我试图为操作编写闭包,并且有效。
public extension UIButton {
public func actionHandleBlock(action:(() -> Void)? = nil) {
struct __ {
static var action :(() -> Void)?
}
if action != nil {
__.action = action
} else {
__.action?()
}
}
@objc public func triggerActionHandleBlock() {
self.actionHandleBlock()
}
public func addAction(forControlEvents control :UIControlEvents, ForAction action:@escaping () -> Void) {
self.actionHandleBlock(action: action)
self.addTarget(self, action: #selector(triggerActionHandleBlock), for: control)
}
}
但是在使用框架实现时,出现以下错误
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton()
btn.addAction(forControlEvents : .touchUpInside,ForAction:{//'addAction' is inaccessible due to 'internal' protection level
})
}