继承自UIControl的控件都可以通过-(void)addTarget:action:forControlEvents的消息进行某些事件处理函数的注册,在不需要的时候还可以通过-(void)removeTarget:action:forControlEvents取消事件处理函数。不过应用开发中在添加时会逐个添加,但在清理时通常则是希望一次性清理干净。但-(void)removeTarget:action:forControlEvents需要传入指定target,因此只能清理指定target的处理函数,而不能清理全部。为此需要对UIControl进行相应的扩张,代码如下:
//Category声明:
@interface UIControl (Additions)
- (void)removeAllTargets;
@end
//Category实现:
@implementation UIControl (Additions)
- (void)removeAllTargets {
for (id target in [self allTargets]) {
[self removeTarget:target action:NULL forControlEvents:UIControlEventAllEvents];
}
}
@end