这是我的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
}()下面是我的函数
func handleRegister(){
FIRAuth.auth()?.createUser(withEmail: email, password: password,completion: { (user, error) in
if error != nil
{ print("Error Occured")}
else
{print("Successfully Authenticated")}
})
}我得到了编译错误,如果addTarget删除,它编译成功
发布于 2016-11-08 17:50:24
是,如果没有参数,不要添加"()“
button.addTarget(self, action:#selector(handleRegister), for: .touchUpInside). 如果你想让发送者
button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside).
func handleRegister(sender: UIButton){
//...
}编辑:
button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside)不再起作用,您需要将选择器中的_替换为您在函数头中使用的变量名,在本例中它将是sender,因此工作代码为:
button.addTarget(self, action:#selector(handleRegister(sender:)), for: .touchUpInside)发布于 2017-11-22 15:04:36
尝试使用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(){
//...
}发布于 2016-09-21 21:29:41
尝尝这个
button.addTarget(self, action:#selector(handleRegister()), for: .touchUpInside). 只需添加带有方法名称的括号即可。
你也可以参考链接:Value of type 'CustomButton' has no member 'touchDown'
https://stackoverflow.com/questions/39617873
复制相似问题