Swift:addGestureRecognizer 在 class 中不起作用
Swift: addGestureRecognizer not work in class
我的class:
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] [ChatService]: SMT:
2018-07-07 18:39:12.470392+0430
Ma[971:260525] [ChatService]: RCV: 2018-07-07 18:39:12.471851+0430
Ma[971:260591] [ChatService]: RCV:
2018-07-07 18:39:14.674675+0430 Ma[971:260392] *** NSForwarding:
warning: object 0x100a9fc70 of class 'Ma.SelectBox' does not implement
methodSignatureForSelector: -- trouble ahead Unrecognized selector
+[Ma.SelectBox handleClickWithSender:] 2018-07-07 18:39:14.675210+0430 Ma[971:260392] Unrecognized selector +[Ma.SelectBox
handleClickWithSender:]
如何设置点击侦听器以通过 class 查看?
谢谢
您的 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 应该如下所示:
class ViewControllerPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var bgGenderInto: UIView!
let box = SelectBox()
override func viewDidLoad() {
super.viewDidLoad()
box.openSelector(list: AppDelegate.genderList, parent: bgGenderInto)
}
}
我的class:
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] [ChatService]: SMT: 2018-07-07 18:39:12.470392+0430 Ma[971:260525] [ChatService]: RCV: 2018-07-07 18:39:12.471851+0430 Ma[971:260591] [ChatService]: RCV: 2018-07-07 18:39:14.674675+0430 Ma[971:260392] *** NSForwarding: warning: object 0x100a9fc70 of class 'Ma.SelectBox' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector +[Ma.SelectBox handleClickWithSender:] 2018-07-07 18:39:14.675210+0430 Ma[971:260392] Unrecognized selector +[Ma.SelectBox handleClickWithSender:]
如何设置点击侦听器以通过 class 查看?
谢谢
您的 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 应该如下所示:
class ViewControllerPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var bgGenderInto: UIView!
let box = SelectBox()
override func viewDidLoad() {
super.viewDidLoad()
box.openSelector(list: AppDelegate.genderList, parent: bgGenderInto)
}
}