简体   繁体   English

如何将观察者添加到UIButton?

[英]How do I add observer to UIButton?

I want be signaled when user will touch button (UIControlEventTouchUpInside). 我想通知用户何时将触摸按钮(UIControlEventTouchUpInside)。 How do I add observer to UIButton? 如何将观察者添加到UIButton?

Look at the documentation of UIControl . 查看UIControl的文档。

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

This method works for anything that inherits from UIControl (including but not limited to UIButtons :) 此方法适用于从UIControl继承的任何东西(包括但不限于UIButtons :)

// add target and action
[myButton addTarget:self 
             action:@selector(buttonClicked:) 
   forControlEvents:UIControlEventTouchUpInside];

where the target is the class where the UIButton is added or implemented. 目标是添加或实现UIButton的类。 If you set nil for addTarget, the action will go through the responder chain until a responder is found that responds to the buttonClicked: selector. 如果将addTarget设置为nil,则该操作将通过响应者链,直到找到响应buttonClicked:选择器的响应者为止。 buttonClicked: selector is implemented like the following: buttonClicked:选择器的实现方式如下:

-(void)buttonClicked:(id)sender
{
 // do stuff here
}

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

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