[英]Issue with implementation of Extension of UIButton
我在多个VC中有一个通话按钮,可以执行对相同号码进行通话的操作。 我没有在每个VC中定义相同的功能,而是决定创建UIButton的扩展。
需要帮助,不确定为什么我会遇到问题,但是在target:self
出现错误target:self
说Expected parameter type following ':'
以下是代码:-
extension UIButton {
func callBtn(target:self)
{
let url = NSURL(string: "tel://1234567890")!
UIApplication.sharedApplication().openURL(url)
}
}
编辑:更新为提到的解决方案:-
extension UIButton {
func callBtn(target:UIButton)
{
let url = NSURL(string: "tel://1234567890)")!
UIApplication.sharedApplication().openURL(url)
}
}
内部必需的VC :-(调用如下)
callBtn.addTarget(self, action: #selector(callBtn.callBtn(_:)), forControlEvents: .TouchUpInside)
出现以下错误:-
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- unrecognized selector sent to instance 0x15e8b330 '
您的代码应该像
extension UIButton {
func callBtn(target:UIButton)
{
let url = NSURL(string: "tel://1234567890")!
UIApplication.sharedApplication().openURL(url)
}
}
如果您要查看错误,则表明无法识别的选择器已发送到实例。 这是因为应该在target
上调用您在按钮的addTarget:
方法中添加的“操作”。 在这里,您给出的是self
,它是UIViewController
类型的对象,并且没有名为callBtn:
的方法。 因此,它将无法识别callBtn:
方法调用。 而是将扩展名从UIButton
更改为UIViewController
,以实现该方法。 因此,每当点击按钮时,它将在目标self
即UIViewController
)上调用您的操作方法( callBtn:
。
extension UIViewController {
func callBtn(sender: UIButton) {
let url = NSURL(string: "tell://1234567890")!
}
}
尝试使用..
extension UIButton {
func callBtn(target:UIButton)
{
let url = NSURL(string: "tel://1234567890")!
UIApplication.sharedApplication().openURL(url)
}
}
您仍然需要任何帮助随时问我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.