MJQ*_*347 34 cocoa-touch objective-c ios swift
我在Swift 3中提出了一个UIButton用Objective-C编写的子类.
当我尝试添加目标时,它失败并显示错误代码:
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
}
}
Run Code Online (Sandbox Code Playgroud)
mat*_*att 41
问题在于#selector(self.action()),self.action()是一个方法调用.你不想调用这个方法; 你想命名方法.#selector(action)相反说:丢失括号,再加上没有必要self.
cjr*_*eck 29
您所要做的就是将func标记为,@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
}
}
Run Code Online (Sandbox Code Playgroud)
private如果你愿意,你甚至可以制作它