[英]Swift4 - UIButton use addTarget in UIView with error
这是UIVIew中required init
中带有addTarget的代码
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
hiddenButton = self.viewWithTag(9000) as? UIButton
hiddenButton.addTarget(self, action: "hiddenCameraAction:", for: .touchUpInside)
}
这是我的选择功能
func hiddenCameraAction(_ sender: Any) {
//Do something
}
当我单击UIView
的按钮时,应用程序崩溃并显示错误:
TeachSystem [27065:8131674] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[TeachSystem.CameraView hiddenCameraAction:]:无法识别的选择器已发送到实例0x121d11050'*第一个抛出调用堆栈:(0x1ee830ec4 0x1eda01a40 0x1ee749c480 0481 0x21b5d5d0c 0x21b5d602c 0x21b5d502c 0x21bb81bac 0x21bb82e10 0x21bb6210c 0x21bc30f68 0x21bc33960 0x21bc2c450 0x1ee7c11f0 0x1ee7c1170 0x1ee7c0a54 0x1ee7bb920 0x1ee7bb1f0 0x1f0a34584 0x21bb46d40 0x105039f40 0x1ee27abb4)的libc ++ abi.dylib:与类型NSException的未捕获的异常终止
问题:如何解决此错误?
操作应定义为#selector
:
hiddenButton.addTarget(self, action: #selector(hiddenCameraAction(_:)), for: .touchUpInside).
应将动作定义为#selector
,并且函数应具有@objc
推断。
hiddenButton.addTarget(self, action: #selector(hiddenCameraAction(_:)), for: .touchUpInside)
hiddenCameraAction函数
@objc func hiddenCameraAction(_ sender: Any) {
//Do something
}
@objc推论允许hidden-CameraAction方法可被Objective-C运行时访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.