在Xcode 7.3中使用#selector指令

时间:2016-05-24 10:53:36

标签: ios xcode swift delegates selector

我有一个协议

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)
    }
}

这给了我编译错误说&#34;&#39; #selector&#39;是指在Objective-C&#34;中未公开的方法。并提出建议&#34;添加&#39; @ obj-c&#39;将此公开给Objective-C&#34;。添加&#39; @ obj-c&#39;另外,我得到同样的错误&#39; @ obj-c&#39;再次添加。问题没有得到解决。

我在Xcode 7.3.1中工作。

早些时候我有

let tapGesture = UITapGestureRecognizer(target: delegate, action: "didTapAnnotation:")

工作正常。将我的Xcode更新为7.3.1后,我遇到了这个问题。

如何将协议的功能设置为选择器?

1 个答案:

答案 0 :(得分:2)

您需要在协议级别添加@objc。

@objc
protocol AnnotationTapDelegate: AnyObject {

    /* Delegate to identify the tap on Annotation view */
    func didTapAnnotation(sender: UITapGestureRecognizer)
}