[英]Issue with implementation of Extension of UIButton
I have a call button in multiple VCs which performs action of calling on same number. 我在多个VC中有一个通话按钮,可以执行对相同号码进行通话的操作。 Instead of defining same func in each VC i decided to create extension of UIButton.
我没有在每个VC中定义相同的功能,而是决定创建UIButton的扩展。
Need help , Not sure why am i facing issue but getting error at target:self
saying Expected parameter type following ':'
需要帮助,不确定为什么我会遇到问题,但是在
target:self
出现错误target:self
说Expected parameter type following ':'
Following is the code:- 以下是代码:-
extension UIButton {
func callBtn(target:self)
{
let url = NSURL(string: "tel://1234567890")!
UIApplication.sharedApplication().openURL(url)
}
}
EDIT: Updated to mentioned solution:- 编辑:更新为提到的解决方案:-
extension UIButton {
func callBtn(target:UIButton)
{
let url = NSURL(string: "tel://1234567890)")!
UIApplication.sharedApplication().openURL(url)
}
}
Inside Required VCs :- (Calling as follows) 内部必需的VC :-(调用如下)
callBtn.addTarget(self, action: #selector(callBtn.callBtn(_:)), forControlEvents: .TouchUpInside)
Getting following error:- 出现以下错误:-
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- unrecognized selector sent to instance 0x15e8b330 '
your code should be like, 您的代码应该像
extension UIButton {
func callBtn(target:UIButton)
{
let url = NSURL(string: "tel://1234567890")!
UIApplication.sharedApplication().openURL(url)
}
}
If you'll look at the error, it says an unrecognized selector has been sent to an instance. 如果您要查看错误,则表明无法识别的选择器已发送到实例。 It is because the 'action' which you add in the
addTarget:
method of the button is supposed to be called on the target
. 这是因为应该在
target
上调用您在按钮的addTarget:
方法中添加的“操作”。 Here you have given as self
which is a object of type UIViewController
and it does not have a method called callBtn:
. 在这里,您给出的是
self
,它是UIViewController
类型的对象,并且没有名为callBtn:
的方法。 So it wont recognize the the callBtn:
method call. 因此,它将无法识别
callBtn:
方法调用。 Instead change the extension from UIButton
to UIViewController
so that it implements the method. 而是将扩展名从
UIButton
更改为UIViewController
,以实现该方法。 So whenever the button is tapped it will call your action method ( callBtn:
) on your target self
which is a UIViewController
. 因此,每当点击按钮时,它将在目标
self
即UIViewController
)上调用您的操作方法( callBtn:
。
extension UIViewController {
func callBtn(sender: UIButton) {
let url = NSURL(string: "tell://1234567890")!
}
}
try using.. 尝试使用..
extension UIButton {
func callBtn(target:UIButton)
{
let url = NSURL(string: "tel://1234567890")!
UIApplication.sharedApplication().openURL(url)
}
}
Still you need any help feel free to ask me. 您仍然需要任何帮助随时问我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.