您的位置:首页 > 移动开发 > IOS开发

如何清除控件的Target-Action

2015-01-30 13:31 288 查看
继承自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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios Target