且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在swift 3中使用addTarget方法

更新时间:2021-11-14 22:28:11

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

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)