im trying to use UISwitch using category. In my catogory class:
@implementation UISwitch(Analytics)
-(void) addTarget:(id)target action:(SEL)action forControlEvents:(UIContr开发者_StackOverflow中文版olEvents)controlEvents{
[super addTarget:target action:action forControlEvents:controlEvents];
}
here i have a problem in capturing the event.
I'm afraid you probably shouldn't be going about it this way. From the docs:
Although the language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from using this functionality.
Among the reasons given for why are:
When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that already existed in the category's class, there is no way to invoke the original implementation.
and
A category cannot reliably override methods declared in another category of the same class. This issue is of particular significance since many of the Cocoa classes are implemented using categories. A framework-defined method you try to override may itself have been implemented in a category, and so which implementation takes precedence is not defined.
Either of those might be in play with your example.
If you really do want to capture all UISwitch behavior for your analytics (rather than just individual ones), you're better off using a custom subclass of UISwitch. If you need to patch this into a large existing project and want to keep using UISwitch as the class name everywhere, you can take try class posing. But that is perhaps a last resort, since it is a fairly heavy-handed technique.
精彩评论