Override addTarget method

646 views Asked by At

I have subclassed UIControl, and would like to override the sendAction:to:forEvent method so that I can send my own customized UIEvent (so that I can retain the UIControlEvents variable). However, since all I have to work with is a UIEvent, I cannot retain the UIControlEvents variable.

Because of this, I figure I need to override the addTarget method so that I can preserve the UIControlEvents variable.


Main Question:

So does anyone know how to implement the addTarget method to preserve the functionality it should without overriding it? Furthermore does anyone know a better solution since apple doesn't give this info (UIControlEvents variable through the UIEvent object) by default.

1

There are 1 answers

1
David H On

In your subclass provide your own addTarget method, but do not call super. Save the target and option, and made yourself the target for that option. When the action method gets sent to your for that event you can take whatever action you want.

EDIT: So you have a subclass, and want to save the added targets etc. Declare a mutable array. When you get this message:

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

in your subclass you add this mutable dictionary as so:

[myArray addObject:[@"target" : target, @"action" : NSStringFromSelector(action), @"event" : [NSValue valueWithBytes:&controlEvents objCType:@encode(controlEvents)]];

While this on first look seems daunting, its not all that complex.

Now we have every registered object that wants to know about every possible control event. Your code can now decide when or if to notify one or more target about any controlEvent by interating through the array and finding each object that want to know about such and such an event.

Your subclass then decides on any event who gets notified about what.