ios - UIButton 扩展的实现问题

标签 ios uibutton swift2

我在多个 VC 中有一个调用按钮,它执行调用相同号码的操作。我没有在每个 VC 中定义相同的函数,而是决定创建 UIButton 的扩展。

需要帮助,不知道为什么我会遇到问题,但在 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)
    }
}

Inside Required VCs :- (调用如下)

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)
}
}

关于ios - UIButton 扩展的实现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39768177/

相关文章:

ios - 快速访问 UIButton 的文本时出现奇怪的错误

ios - UITableViewCell didSelectRowAt : not getting called when press on UIButton area in cell

Swift for-in 循环与枚举自定义 Array2D 类?

ios - 如何使用 NSNumberFormatter 只显示前两位数字?

ios - 解析 : PFObject is loaded without Attributes

ios - Mac 上的 Corona 音频错误? audio.setMaxVolume( ) 在 mac 上不工作?

ios - 如何检查 iOS 设置是否允许权限

arrays - 切换数组中的 UI 按钮以选择/取消选择

swift - 包中的 info.plist 必须包含 CFBundleVersion key

ios - UIWebView 有 UIToolbar 吗?