Ios UIButton扩展的实现问题

Ios UIButton扩展的实现问题,ios,uibutton,swift2,Ios,Uibutton,Swift2,我在多个VCs中有一个呼叫按钮,执行呼叫相同号码的操作。我决定创建UIButton的扩展,而不是在每个VC中定义相同的func 需要帮助,不确定我为什么会遇到问题,但在target:self说以下是预期参数类型“:” 代码如下:- extension UIButton { func callBtn(target:self) { let url = NSURL(string: "tel://1234567890")! UIApplication.sh

我在多个VCs中有一个呼叫按钮,执行呼叫相同号码的操作。我决定创建UIButton的扩展,而不是在每个VC中定义相同的func

需要帮助,不确定我为什么会遇到问题,但在
target:self
以下是预期参数类型“:”

代码如下:-

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)
    }
}
所需VCs内部:-(调用如下)

获取以下错误:-

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)
        }
    }
您仍然需要任何帮助,请尽管问我。

尝试使用

  extension UIButton {
        func callBtn(target:UIButton)
        {
            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)
}
}

你的代码应该是

extension UIButton {
func callBtn(target:UIButton)
{
    let url = NSURL(string: "tel://1234567890")!
    UIApplication.sharedApplication().openURL(url)
}
}

如果您查看该错误,它表示已将无法识别的选择器发送到实例。这是因为您在按钮的
addTarget:
方法中添加的“操作”应该在
目标上调用。这里您给出的是
self
,它是
UIViewController
类型的对象,并且没有名为
callBtn:
的方法。因此它无法识别
callBtn:
方法调用。而是将扩展名从
UIButton
更改为
UIViewController
,以便实现该方法。因此,每当点击按钮时,它都会在目标
self
上调用您的操作方法(
callBtn:
),这是一个
UIViewController

extension UIViewController {
    func callBtn(sender: UIButton) {
        let url = NSURL(string: "tell://1234567890")!
    }
}

如果您查看该错误,它表示已将无法识别的选择器发送到实例。这是因为您在按钮的
addTarget:
方法中添加的“操作”应该在
目标上调用。这里您给出的是
self
,它是
UIViewController
类型的对象,并且没有名为
callBtn:
的方法。因此它无法识别
callBtn:
方法调用。而是将扩展名从
UIButton
更改为
UIViewController
,以便实现该方法。因此,每当点击按钮时,它都会在目标
self
上调用您的操作方法(
callBtn:
),这是一个
UIViewController

extension UIViewController {
    func callBtn(sender: UIButton) {
        let url = NSURL(string: "tell://1234567890")!
    }
}

您如何尝试调用此方法!实际上,我添加了3个参数:目标、动作和控制。将调用类似于
btn.addTarget(btn.callBtn)
@Vishnuvardhan:-以下是我调用方法的方式:-
callBtn.addTarget(self,action:#选择器(callBtn.callBtn(:)),forControlEvents:.toucupinside)
insideredVC@EricAya这是我的真实代码,只有手机号码改变了。你怎么试着调用这个方法!实际上,我添加了3个参数:目标、动作和控制。将调用类似于
btn.addTarget(btn.callBtn)
@Vishnuvardhan:-以下是我调用方法的方式:-
callBtn.addTarget(self,action:#选择器(callBtn.callBtn(:)),forControlEvents:.toucupinside)
insideredVC@EricAya这是我真正的密码,只更改了手机号码。您能告诉我为什么
self
不工作吗?。self和UIButton在本例中与在UIButton扩展中使用的相同。现在,如何使用该扩展?您能告诉我为什么
self
不起作用吗?。self和UIButton在本例中与在UIButton的扩展中使用的相同。现在,如何使用该扩展?现在,如何使用该扩展?现在,如何使用该扩展?