Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine which UIControlEvents type caused a UIEvent?

What I want to do is set up a bunch of UIControl objects to send all events through the same handler. That handler than needs to determine what the appropriate action is based on the UIControlEvents type that triggered.

- (void)handleEventFromControl:(id)sender withEvent:(UIEvent *)event {
    UIControl *control = sender;
    NSIndexPath *indexPath = [controlIndexPaths objectForKey:control];
    UIControlEvents controlEventType = event.type; //PROBLEM HERE

    BOOL fire = NO;
    NSNumber *eventCheck = [registeredEventsByToolkitIndex objectAtIndex:indexPath.toolkit];
    fire = ([eventCheck integerValue] & controlEventType);

    if(!fire) {
        eventCheck = [registeredEventsByControlIndex objectAtIndex:indexPath.control];
        fire = ([eventCheck integerValue] & controlEventType);
    }

    if(!fire) {
        eventCheck = [registeredEventsByIndexPath objectForKey:indexPath];
        fire = ([eventCheck integerValue] & controlEventType);
    }

    if(fire) {
        [self.delegate toolkit:self indexPath:indexPath firedEvent:event];
    }
}

As best as I can tell, event.type doesn't work this way. I want to avoid having a subclass of UIControl because that is severe overkill for what I want to do. The rest of the moving parts here aren't really that important; what I want is a way to check against UIControlEvents after the target:action: pair is used in the following setup:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

If there is a different way to setup of a target:action: that sends back the UIControlEvents, I'd take that. But I am not seeing it in the documentation. Or, at the very least, get access outside of the UIControl object such that I can get the type I want.

like image 970
MrHen Avatar asked May 25 '11 22:05

MrHen


1 Answers

You're correct; UIEvent.type does not work that way. Internally, UIControl subclasses are basically using the sendActionsForControlEvents: method, which means they can choose to put anything they want as the parameter.

As for how to know, the simple answer is to have a separate method for each control event.

like image 56
Dave DeLong Avatar answered Oct 11 '22 02:10

Dave DeLong