I have a call button in multiple VCs which performs action of calling on same number. Instead of defining same func in each VC i decided to create extension of UIButton.
Need help , Not sure why am i facing issue but getting error at target:self
saying 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)
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
. Here you have given as self
which is a object of type UIViewController
and it does not have a method called callBtn:
. So it wont recognize the the callBtn:
method call. Instead change the extension from UIButton
to UIViewController
so that it implements the method. So whenever the button is tapped it will call your action method ( callBtn:
) on your target self
which is a UIViewController
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.