如何在swift 3中使用addTarget方法

Nin*_*a13 54 selector ios swift

这是我的button对象

    let loginRegisterButton:UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = UIColor(r: 50 , g: 80, b: 130)
    button.setTitle("Register", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitleColor(.white, for: .normal)
    button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside)
    return button
}()
Run Code Online (Sandbox Code Playgroud)

这是我的功能

    func handleRegister(){

    FIRAuth.auth()?.createUser(withEmail: email, password: password,completion: { (user, error) in

        if error != nil
        { print("Error Occured")}

        else
        {print("Successfully Authenticated")}
    })        
}
Run Code Online (Sandbox Code Playgroud)

我收到编译错误,如果addTarget删除它成功编译

Dam*_*ito 101

是的,如果没有参数,请不要添加"()"

button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside). 
Run Code Online (Sandbox Code Playgroud)

如果你想得到发件人

button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside). 

func handleRegister(sender: UIButton){
   //...
}
Run Code Online (Sandbox Code Playgroud)

编辑:

button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)

不再有效,你需要_在选择器中替换你在函数头中使用的变量名,在这种情况下它将是sender,所以工作代码变为:

button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)
Run Code Online (Sandbox Code Playgroud)


Sou*_*men 19

尝试使用Swift 4

buttonSection.addTarget(self, action: #selector(actionWithParam(_:)), for: .touchUpInside)
@objc func actionWithParam(sender: UIButton){
    //...
}

buttonSection.addTarget(self, action: #selector(actionWithoutParam), for: .touchUpInside)
@objc func actionWithoutParam(){
    //...
}
Run Code Online (Sandbox Code Playgroud)

  • 作为Apple官方文档(https://developer.apple.com/documentation/swift/using_objective_c_runtime_features_in_swift),您需要使用@objc来调用您的选择器方法,而不是使用选择器的其他"纯粹快速"方式. (5认同)

VRA*_*ome 13

试试这个

button.addTarget(self, action:#selector(handleRegister()), for: .touchUpInside). 
Run Code Online (Sandbox Code Playgroud)

只需添加带有方法名称的括号即可.

你也可以参考链接:'CustomButton'类型的值没有成员'touchDown'


era*_*del 7

尝试使用swift 3

cell.TaxToolTips.tag = indexPath.row
        cell.TaxToolTips.addTarget(self, action: #selector(InheritanceTaxViewController.displayToolTipDetails(_:)), for:.touchUpInside)


 @objc func displayToolTipDetails(_ sender : UIButton) {
        print(sender.tag)
        let tooltipString = TaxToolTipsArray[sender.tag]
        self.displayMyAlertMessage(userMessage: tooltipString, status: 202)    
}
Run Code Online (Sandbox Code Playgroud)


Gia*_*ang 7

  let button: UIButton = UIButton()
    button.setImage(UIImage(named:"imagename"), for: .normal)
    button.addTarget(self, action:#selector(YourClassName.backAction(_sender:)), for: .touchUpInside)

    button.frame = CGRect.init(x: 5, y: 100, width: 45, height: 45)
    view.addSubview(button)

    @objc public func backAction(_sender: UIButton) {

    }
Run Code Online (Sandbox Code Playgroud)