[英]Swift4 - UIButton use addTarget in UIView with error
This is code with addTarget in required init
in UIVIew 这是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)
}
this is my select function 这是我的选择功能
func hiddenCameraAction(_ sender: Any) {
//Do something
}
when I click the button in UIView
then application crashes with error : 当我单击
UIView
的按钮时,应用程序崩溃并显示错误:
TeachSystem[27065:8131674] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TeachSystem.CameraView hiddenCameraAction:]: unrecognized selector sent to instance 0x121d11050' * First throw call stack: (0x1ee830ec4 0x1eda01a40 0x1ee749c24 0x21bb74558 0x1ee8367dc 0x1ee83848c 0x21bb48454 0x21b5d5d0c 0x21b5d602c 0x21b5d502c 0x21bb81bac 0x21bb82e10 0x21bb6210c 0x21bc30f68 0x21bc33960 0x21bc2c450 0x1ee7c11f0 0x1ee7c1170 0x1ee7c0a54 0x1ee7bb920 0x1ee7bb1f0 0x1f0a34584 0x21bb46d40 0x105039f40 0x1ee27abb4) libc++abi.dylib: terminating with uncaught exception of type NSException
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的未捕获的异常终止
Question : How to resolve this error? 问题:如何解决此错误?
操作应定义为#selector
:
hiddenButton.addTarget(self, action: #selector(hiddenCameraAction(_:)), for: .touchUpInside).
All you need to do is to put string into parentheses. 您需要做的就是将字符串放在括号中。
action: ("hiddenCameraAction:")
... however, this is deprecated way how to achieve what you need to achieve. ...但是,这是过时的方式,无法实现您需要实现的目标。
I would recommend you to start using selector which is safer since compiler provides you error immediately and code won't run with bad method name or whatever. 我建议您开始使用选择器,该选择器更安全,因为编译器会立即为您提供错误,并且代码不会以错误的方法名称或任何其他方式运行。
Syntax is: #selector(method(externalParameter:))
语法为:
#selector(method(externalParameter:))
... you can just start typing and compiler will suggest you what Objective-C method you can put inside ...您可以开始输入内容,编译器会建议您可以放入哪种Objective-C方法
action: #selector(hiddenCameraAction(_:))
Action should be defined as #selector
and the function should have an @objc
inference. 应将动作定义为
#selector
,并且函数应具有@objc
推断。
hiddenButton.addTarget(self, action: #selector(hiddenCameraAction(_:)), for: .touchUpInside)
hiddenCameraAction function hiddenCameraAction函数
@objc func hiddenCameraAction(_ sender: Any) {
//Do something
}
The @objc inference allows the hiddenCameraAction method be accessible to the Objective-C runtime. @objc推论允许hidden-CameraAction方法可被Objective-C运行时访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.