我希望在用户触摸按钮(UIControlEventTouchUpInside)时发出信号。如何将观察者添加到UIButton?
答案 0 :(得分:4)
查看UIControl的文档。
[myButton addTarget:self
action:@selector(touch:)
forControlEvents:UIControlEventTouchUpInside];
此方法适用于从UIControl
继承的任何内容(包括但不限于UIButtons
:)
答案 1 :(得分:2)
// add target and action
[myButton addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
其中target是添加或实现UIButton的类。如果为addTarget设置nil,则操作将通过响应程序链,直到找到响应buttonClicked:选择器的响应程序。 buttonClicked:选择器的实现方式如下:
-(void)buttonClicked:(id)sender
{
// do stuff here
}