本文介绍了'#selector'的参数不是指'@objc'方法,属性或初始值设定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Swift 3中使用Objective-C编写了一个 UIButton
子类。
I sublcassed in Swift 3 a UIButton
subclass that is written in Objective-C.
当我尝试要添加目标,它会失败并显示错误代码:
When I try to add a target, it fails with an error code:
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
}
}
推荐答案
您所要做的就是将func标记为 @objc
,并且不需要 self
引用或括号
All you have to do is mark the func as @objc
and there's no need for the self
reference or the parenthesis
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
}
}
您甚至可以将它设为私人
如果你想要
You can even make it private
if you want
这篇关于'#selector'的参数不是指'@objc'方法,属性或初始值设定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!