gpt4 book ai didi

ios - 如何在swift 3中使用addTarget方法

转载 作者:IT王子 更新时间:2023-10-29 04:59:27 24 4
gpt4 key购买 nike

这是我的按钮对象

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

如果你想得到发件人

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)

关于ios - 如何在swift 3中使用addTarget方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39617873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com