当更新项目代码到swift 4时,add.target方法会出现一些错误 我怎么能解决这个错误?
//swift3
var chatLogController: ChatLogController? {
didSet {
sendButton.addTarget(chatLogController, action: #selector(ChatLogController.handleSend), for: .touchUpInside)
uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: chatLogController, action: #selector(ChatLogController.handleUploadTap)))
}
}
答案 0 :(得分:9)
提示消息告诉您该怎么做,在声明您的功能之前添加@obj
。
@objc func handleSend(_ sender: UIGestureRecognizer){
...
}
原因是:
在Objective-C中,选择器是一个引用名称的类型 Objective-C方法。在Swift中,Objective-C选择器由
Selector
结构,可以使用#selector
构建 表达。为可以从中调用的方法创建选择器 Objective-C,传递方法的名称,如#selector(MyViewController.tappedButton(_:))
。要为属性的Objective-C getter或setter方法构造选择器,请传递 属性名称以getter:
或setter:
标签为前缀,例如#selector(getter: MyViewController.myButton)
。
在Apples文档中阅读更多here。