ios - 如何将 addTarget 方法更新为 swift 4

标签 ios swift xcode selector swift4

<分区>

当将项目代码更新为 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)))
    }
}

最佳答案

提示消息告诉你该做什么,在声明你的函数之前添加@obj

@objc func handleSend(_ sender: UIGestureRecognizer){
    ...
}

原因是因为:

In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, Objective-C selectors are represented by the Selector structure, and can be constructed using the #selector expression. To create a selector for a method that can be called from Objective-C, pass the name of the method, such as #selector(MyViewController.tappedButton(_:)). To construct a selector for a property’s Objective-C getter or setter method, pass the property name prefixed by the getter: or setter: label, such as #selector(getter: MyViewController.myButton).

阅读更多 here , 在 Apple 文档中。

关于ios - 如何将 addTarget 方法更新为 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46392832/

相关文章:

objective-c - 关于 NSString 的 iOS 内存管理

iphone - 在通用应用程序中使用 UIPopoverController

swift - 如何使用 SwiftUI 在 if 语句中创建 var

ios - iOS5.1 中的位置服务无法正常工作

swift - uicollectionview 水平计数以制作日历

swift - 在 Swift 中使用 slider 控制 ciFilter

ios - 如何从自定义 View 中访问 View Controller 中的变量?

ios - XCode中带有环境变量的条件链接静态库

ios - 在 Xcode 中使用库

ios - Swift 导航栏正在更改状态栏文本颜色