I am trying to figure out how to log the UIControlEvents so that I can see exactly which ones I want to add to my UIButton.
I can't seem to figure it out. Here is what I've tried so far:
[button addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventAllEvents];
-(void)buttonTouched:(UIButton *)button forEvent:(UIControlEvents)event{
NSLog(@"%s", __PRETTY_FUNCTION__);
switch (event) {
case UIControlEventTouchDown:
NSLog(@"UIControlEventTouchDown");
break;
case UIControlEventTouchDownRepeat:
NSLog(@"UIControlEventTouchDownRepeat");
break;
case UIControlEventTouchDragInside:
NSLog(@"UIControlEventTouchDragInside");
break;
case UIControlEventTouchDragOutside:
NSLog(@"UIControlEventTouchDragOutside");
break;
case UIControlEventTouchDragEnter:
NSLog(@"UIControlEventTouchDragEnter");
break;
case UIControlEventTouchDragExit:
NSLog(@"UIControlEventTouchDragExit");
break;
case UIControlEventTouchUpInside:
NSLog(@"UIControlEventTouchUpInside");
break;
case UIControlEventTouchUpOutside:
NSLog(@"UIControlEventTouchUpOutside");
break;
case UIControlEventTouchCancel:
NSLog(@"UIControlEventTouchCancel");
break;
case UIControlEventValueChanged:
NSLog(@"UIControlEventValueChanged");
default:
break;
}
}
The only log that is printed is the __PRETTY_FUNCTION__
log. I also tried this, just in case the switch statement was the problem:
if (event == UIControlEventTouchDown) {
NSLog(@"UIControlEventTouchDown");
}
if (event == UIControlEventTouchDownRepeat){
NSLog(@"UIControlEventTouchDownRepeat");
}
etc...
Still nothing logs. What am I doing wrong?
Edit: I experimented further by changing which event I add to the button like so:
[button addTarget:self action:@selector(buttonTouched:forEvent:) forControlEvents:UIControlEventTouchDown];
And then I logged the input very specifically:
-(void)buttonTouched:(UIButton *)button forEvent:(UIControlEvents)event{
NSLog(@"UIControlEventTouchDown = %i", UIControlEventTouchDown);
NSLog(@"event = %i", event);
}
Here is the output:
UIControlEventTouchDown = 1
event = 350486320
Does anyone have any idea what is going on?
Edit 2: Thanks to TAKeanice I now realize that I hadn't written the selector correctly. I'm not sure exactly what the log for event was printing out, but while I had essentially cast a UIEvent
as an enum type (UIControlEvents
) it still was giving a value in the log.
Anyhow, it seems to me that there must still be a way to find out what the UIControlEvents is, but I can't see how from the documentation.
However, Apple alludes to it in the UIControl documentation:
Subclassing Notes You may want to extend a UIControl subclass for either of two reasons:
To observe or modify the dispatch of action messages to targets for particular events To do this, override sendAction:to:forEvent:, evaluate the passed-in selector, target object, or UIControlEvents bit mask, and proceed as required.
To provide custom tracking behavior (for example, to change the highlight appearance) To do this, override one or all of the following methods: beginTrackingWithTouch:withEvent:, continueTrackingWithTouch:withEvent:, endTrackingWithTouch:withEvent:
How can you evaluate the UIControlEvents
bit mask when the argument passed in for sendAction:to:forEvent:
is UIEvent
?
Any ideas?