Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIControl: sendActionsForControlEvents omits UIEvent

I want to implement a custom subclass of UIControl. It works beautifully except for one fatal problem that is making me spit teeth. Whenever I use sendActionsForControlEvents: to send an action message out, it omits to include a UIEvent. For example, if I link it to a method with the following signature:

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

... the event always comes back as nil! The problem seems to occur within sendActionsForControlEvents:

Now, I need my IBAction to be able to determine the location of the touch. I usually do so by extracting the touches from the event. Surely there must be some way to ensure that the correct event is delivered? It's a pretty fundamental part of using a UIControl!

Anyone know a legal workaround?

like image 349
Ash Avatar asked Jul 08 '12 11:07

Ash


1 Answers

I would assume that this is because the sendActionsForControlEvents: method can't know which UIEvent (if any) your control event should be associated with.

You could try to send all the actions separately (replicating what the sendActionsForControlEvents: method does, according to the documentation), so you can specifically associate them with a 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];
    }
}
like image 160
omz Avatar answered Oct 11 '22 23:10

omz