When defining a callback for a UIButton I listed several events for the same action
In the target I would like to be able to distinguish what event triggered the callback
[button addTarget:self action:@selector(callback:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchCancel];
-(void)callback:(UIButton *)button
{
// need to be able to distinguish between the events
if (event == canceled)
{
}
if (event == touchDown)
{
}
... etc
}
You can change your action to take the event parameter, like this:
Adding a second parameter to your callback will make Cocoa pass the event to you, so that you could check what has triggered the callback.
EDIT : Unfortunately, cocoa does not send you a
UIControlEvent
, so figuring out what control event has caused the callback is not as simple as checking the event type. TheUIEvent
provides you a collection of touches, which you can analyze to see if it's aUITouchPhaseCancelled
touch. This may not be the most expedient way of doing things, though, so setting up multiple callbacks that channel the correct type to you may work better:If you need different behaviors for each event, you should consider writing a different callback for each one.
You could call the callback from a third / fourth method that knows about the control Event:
Better to do the below:
And of course: