开发者

How do I really know when a UIButton has been released?

开发者_如何转开发I'm running into an issue where I have a UIButton in which I want to track press/release events. So I am tacking on to the Touch Down/Touch Up events. Unfortunately, in certain situations this won't track Touch Up events -- for instance, if an Alert pops up.

- (IBAction)buttonDown:(id)sender;
- (IBAction)buttonUp:(id)sender;

How do I ensure that the view controller is notified when the button goes from pushed/normal state, no matter what the reason?


Try this method:

[myButton addTarget: target action: @selector(buttonUp:) forControlEvents: UIControlEventTouchUpInside];


You need to track the touch cancel event as well. That should do it.

Edit: Hmmm, what about UIControlEventAllTouchEvents? You have to add it programmatically. I'm really surprised cancel didn't do it. You could also try handling -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event in your UIViewController.

[myButton addTarget:self action:@selector(allTouches) forControlEvents:UIControlEventAllTouchEvents];

Excellent.


Are you trying to detect if the buttons are being pressed or released while the UIAlert view is currently on top or has it been dismissed and the button presses are no longer responsive?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜