我正在以静态方法编程创建一个UIView,并希望添加UITapGestureRecognizer,它将在我的Helper类中调用另一个静态方法.
Helper.swift:
static func showLoadingPopUp(frame: CGRect) -> UIView {
let transView = UIView.init(frame: frame!)
let tapGesture = UITapGestureRecognizer(target: self, action: "transViewTapped:")
transView.addGestureRecognizer(tapGesture)
return transView
}
static func transViewTapped(gesture: UITapGestureRecognizer) {
print("Oh Tapped!!!")
}
但它最终会出现以下消息,可能是因为我的方法的静态特性.另外Helper.swift是简单的swift类(不是UIView或UIViewController)
错误:
Unrecognized selector +[JaClassified.Helper transViewTapped:]
最佳答案 如果你的类不是NSObject的后代,你可能需要将静态函数桥接到Objective-C(因为旧式的Objective-C选择器适用于NSObjects).
因此,在您的情况下,您可以将Helper类声明为
class Helper: NSObject {
...
}
或者,您可以通过前缀@objc将transViewTapped:桥接到Objective-C
@objc static func transViewTapped(gesture: UITapGestureRecognizer) {
...
}
希望能帮助到你.