Swift:addGestureRecognizer 在课堂上不起作用



我的类:

class SelectBox {
internal static func openSelector(list:[String: String], parent:UIView){
print("iosLog HELLO")
parent.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClick(sender:))))
}
@objc func handleClick(sender: UITapGestureRecognizer) {
print("iosLog CLICK")
}
}

设置视图 :

SelectBox.openSelector(list: AppDelegate.stateList, parent: bgPlaceInto)

启动打印HELLO后,但单击view后,我得到以下错误:

2018-07-07 18:39:12.298322+0430 Ma[971:260558] [聊天服务]: SMT: 2018-07-07 18:39:12.470392+0430 Ma[971:260525] [聊天服务]: RCV: 2018-07-07 18:39:12.471851+0430 马[971:260591] [聊天服务]: RCV: 2018-07-07 18:39:14.674675+0430 Ma[971:260392] *** NS污染: 警告:类"Ma.SelectBox"的对象0x100a9fc70未实现 方法签名选择器: -- 前面有问题 无法识别的选择器 +[Ma.SelectBox handleClickWithSender:] 2018-07-07 18:39:14.675210+0430 Ma[971:260392] 无法识别的选择器 +[Ma.SelectBox handleClickWithSender:]

如何设置按类查看的点击监听器?

谢谢

您的openSelector方法是静态的。静态上下文中的单词self是指周围类型的元类型的实例。在这种情况下,SelectorBox.Type.

显然,SelectorBox.Type没有handleClick的方法。SelectorBox确实如此。

您需要使openSelector方法非静态:

internal func openSelector(list:[String: String], parent:UIView){
print("iosLog HELLO")
parent.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClick(sender:))))
}

现在self指的是SelectorBox实例。

你可以这样称呼它:

// declare this at class level:
let box = SelectorBox()
// call the method like this
box.openSelector()

编辑:你的类应该看起来像这样:

class ViewControllerPage: UIViewController, UITableViewDelegate, UITableViewDataSource { 
@IBOutlet var bgGenderInto: UIView!
let box = SelectBox()  
override func viewDidLoad() { 
super.viewDidLoad() 
box.openSelector(list: AppDelegate.genderList, parent: bgGenderInto) 
}
}

最新更新