ios - #selector 指令在 Xcode 7.3 中的使用

标签 ios xcode swift delegates selector

我有一个协议(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/

相关文章:

ios - 为 xcode 项目设置默认字体样式、颜色和大小

ios - CLLocationManager 总是崩溃并返回 nil 值

ios - 使用 spritekit 无法进行碰撞检测

ios - Grouped Table View 发送 nil 但 Plain 完美运行

ios - 无法正确设置 TableViewCell 的行高

ios - 如何确定用户是否已使用 Graph API 从 Facebook iOS 注销?

ios - 如何在ios中针对不同尺寸类别使用相同的Outlet?

ios - 自定义 ViewController 转换 : Get ToViewController's Subview's Frame

ios - 仅针对 Xcode 单元测试未找到 COCOAPOD 文件

ios - 如何设置 SKCameraNode 相对于节点的 x 和 y 位置?