【发布时间】:2017-01-29 18:46:00
【问题描述】:
这是我的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 删除它编译成功
【问题讨论】:
-
试试这个
button.addTarget(self, action:#selector(handleRegister()), for: .touchUpInside)。 -
handleRegister动作在同一个控制器中?你也没有设置UIButton的框架。 -
是的,我确实有另一个用于约束的函数 func constraints() {loginRegisterButton.centerXAnchor.constraint(equalTo: inputview.centerXAnchor).isActive = true loginRegisterButton.topAnchor.constraint(equalTo: inputview. bottomAnchor, 常量:12).isActive = true loginRegisterButton.widthAnchor.constraint(equalTo: inputview.widthAnchor).isActive = true loginRegisterButton.heightAnchor.constraint(equalToConstant: 30).isActive = true }
-
@Ninja13 那么问题可能是您的按钮约束尝试设置一次框架并检查它是否正常工作。
-
我改变了这样的代码,它工作很懒 var 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)返回按钮 }()