14

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.

MrHen
  • 2,420
  • 2
  • 25
  • 39
  • 1
    possible duplicate of http://stackoverflow.com/questions/2437875/target-action-uicontrolevents – albertamg May 25 '11 at 22:34
  • @albertamg: Yes, that question exactly covers the same ground as this one. Any voters passing through, go ahead and close this. – MrHen May 25 '11 at 22:39

1 Answers1

20

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.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498