UIControl: sendActionsForControlEvents excludes UIEvent

I want to implement a custom subclass of UIControl. It works great, except for one fatal problem that makes me spit teeth. Whenever I use sendActionsForControlEvents: to send an action message, it should not include UIEvent. For example, if I bind it to a method with the following signature:

- (IBAction) controlTouched:(id)sender withEvent:(UIEvent *)event

... the event always returns as zero! The problem occurs in sendActionsForControlEvents:

Now I need my IBAction to be able to determine the location of the touch. I usually do this by extracting the touch from the event. Is there really got to be some way to ensure the right event? This is a pretty fundamental part of using UIControl!

Does anyone know a legal solution?

+5
source share
2 answers

I would suggest that this is due to the fact that the method sendActionsForControlEvents:cannot know to which UIEvent(if any) your control event should be associated.

You can try to send all the actions separately (replicating what the method does sendActionsForControlEvents:, as per the documentation), so you can link them with UIEvent:

UIEvent *event = ...;
UIControlEvents controlEvent = ...;

for (id target in [self allTargets]) {
    NSArray *actions = [self actionsForTarget:target forControlEvent:controlEvent];
    for (NSString *action in actions) {
        [self sendAction:NSSelectorFromString(action) to:target forEvent:event];
    }
}
+4
source

I have one possible solution at the moment, but I'm not very happy with that. For others facing the same problem, though, here it is. First declare a local variable or property for UIEvent this way:

@property (nonatomic, assign) UIEvent * currentEvent;

, , UIEvent [self sendActionsForControlEvents:], , UIControlEventTouchDown , , .

self.currentEvent = event;
[self sendActionsForControlEvents: UIControlEventTouchDown];

, :

- (void) sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    [super sendAction:action to:target forEvent:self.currentEvent];
}

, , , - , UIEvent, !

0