我在Swift 3中提出了一个用Objective-C编写的UIButton
子类。
当我尝试添加目标时,它失败并显示错误代码:
class ActionButton: JTImageButton {
func action() {
}
func configure()) {
// ...
self.addTarget(self, action: #selector(self.action()), for: .touchUpInside)
// error:
// Argument of '#selector' does not refer to an '@objc' method, property, or initializer
}
}
您所要做的就是将函数标记为@objc
,并且不需要self
引用或括号
class ActionButton: JTImageButton {
@objc func btnAction() {
}
func configure() {
// ...
self.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
// error:
// Argument of '#selector' does not refer to an '@objc' method, property, or initializer
}
}
如果你愿意,你甚至可以把它变成private
问题是在#selector(self.action())
中,self.action()
是一种方法调用。你不想调用这个方法;你想命名方法。再说#selector(action)
:丢失括号,再加上不需要self
。
从另一个答案的评论中添加:func action()不仅仅是函数名称和操作的糟糕选择,它无法构建。 (你可以使用它作为输入函数参数,为了清楚起见,我将目标/动作传递给设置这些东西的init()。)为了清楚起见,我用MyAction()替换它。
试试这个:
self.addTarget(self, action: #selector(MyAction), for: .touchUpInside)
说,一个更好的设计是将MyAction()函数移动到按钮superview,因为这使得事物更符合基本的MVC设计:
上海华:
let myButton = ActionButton()
// include other button setup here
myButton.addTarget(self, action: #selector(MyAction), for: .touchUpInside
func action(_ sender: ActionButton) {
// code against button tap here
}
替代编码,在视图控制器中保留“action()”方法,但只将“addTarget”移动到按钮中:
self.addTarget(superview?, action(superview?.MyAction), for: .touchUpInside)
为什么我要求您考虑将“MyAction()”方法移至superview?双重:
而不是说self.action()
,使用ActionButton.action()
。
如果您不介意添加额外的功能,可以嵌套该功能。
self.addTarget(self, action: myAction, for: UIControlledEvent)
myAction(){
@obj.methodYouWantToCall(//parameters//)
}