我有一个协议(protocol)
protocol AnnotationTapDelegate: AnyObject {
/* Delegate to identify the tap on Annotation view */
func didTapAnnotation(sender: UITapGestureRecognizer)
}
和一个类
class CustomAnnotationView: MKAnnotationView {
func setTapDelegate(delegate: AnnotationTapDelegate!) {
let tapGesture = UITapGestureRecognizer(target: delegate, action: #selector(AnnotationTapDelegate.didTapAnnotation(_:))) <== Error
self.addGestureRecognizer(tapGesture)
}
}
这给了我编译错误,说“'#selector' 的参数指的是 Objective-C 中未公开的方法”,并建议“添加'@obj-c'以将其公开给 Objective-C”。在添加 '@obj-c' 之后,我得到了同样的错误并且再次添加了 '@obj-c'。问题没有得到解决。
我在 Xcode 7.3.1 中工作。
我之前有
let tapGesture = UITapGestureRecognizer(target: delegate, action: "didTapAnnotation:")
工作正常。将我的 Xcode 更新到 7.3.1 后,我遇到了这个问题。
如何将协议(protocol)的功能设置为选择器?
最佳答案
您需要在协议(protocol)级别添加@objc。
@objc
protocol AnnotationTapDelegate: AnyObject {
/* Delegate to identify the tap on Annotation view */
func didTapAnnotation(sender: UITapGestureRecognizer)
}
关于ios - #selector 指令在 Xcode 7.3 中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411479/