简体   繁体   English

UIButton动作目标方法之间的区别

[英]Difference between UIButton action target methods

What is the difference between option 1 and 2? 选项1和2有什么区别? I am programatically setting up buttons and would like to know what is the advantage of one over the other or if they simply produce the same just different methods of reaching the goal. 我正在以编程方式设置按钮,并且想知道一个按钮相对于另一个按钮的优点是什么,或者它们只是产生相同的目标方法而已。

Option 1 : 选项1 :

[signupButton addTarget:self action:@selector(signupButton:) forControlEvents:UIControlEventTouchUpInside];

Followed by the call: 依次致电:

-(IBAction)signupButton:(id)sender{

AND Option 2: AND选项2:

[signupButton addTarget:self action:@selector(signupButtonMethod) forControlEvents:UIControlEventTouchUpInside];

Followed by the call: 依次致电:

-(void)signupButtonMethod {

The first form includes the button as an argument to the method. 第一种形式包括按钮作为方法的参数。 This is useful when you wish to use the same action for more than one button. 当您希望对多个按钮使用同一操作时,此功能很有用。 Then you can make use of the sender argument to know which button triggered the event. 然后,您可以使用sender参数来了解哪个按钮触发了事件。

The second form doesn't include any information about which button triggered the event. 第二种形式不包含有关哪个按钮触发了事件的任何信息。 This is fine if the method is only used for a single button. 如果该方法仅用于单个按钮,则很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM