如何在swift 3中使用addTarget方法

编程入门 行业动态 更新时间:2024-10-08 23:02:35
本文介绍了如何在swift 3中使用addTarget方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的按钮对象

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删除它compi成功

I'm getting compile error, if addTarget removed it compiles successfully

推荐答案

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

Yes, don't add "()" if there is no param

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 ,因此工作代码变为:

no longer works, you need to replace _ in the selector with a variable name you used in the function header, in this case it would be sender, so the working code becomes:

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

更多推荐

如何在swift 3中使用addTarget方法

本文发布于:2023-11-26 21:55:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   如何在   swift   addTarget

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!