逆钟夕的忘忧阁

有朋自远方来,不亦乐乎?

IOS 关于取消延迟执行函数的种种。performSelector与cancelPreviousPerformRequestsWithTarget

@interface NSObject (NSDelayedPerforming)  
  
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;  
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;  
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument;  
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;  
  
@end  


最近看到大家对cancelPreviousPerformRequestsWithTarget不能取消带参数的
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
情况,写了一点点代码调试了一下,发现如果是带参数,那取消时的参数也要一致,否则不能取消成功。
开始延迟执行:


[self performSelector:@selector(didRuninCurrModel:) withObject:[NSNumber numberWithBool:YES] afterDelay:3.0f];  
  
[self performSelector:@selector(didRuninCurrModelNoArgument) withObject:nil afterDelay:3.0f];  


假如在三秒内执行以下取消方法,则结果分别为:


[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(didRuninCurrModel:) object:[NSNumber numberWithBool:YES]];//可以取消成功。  
  
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(didRuninCurrModel:) object:[NSNumber numberWithBool:NO]];//不能取消成功。参数不匹配  
  
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(didRuninCurrModel:) object:nil];//不能取消成功。参数不匹配  
  
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(didRuninCurrModelNoArgument) object:nil];//可以成功取消  
  
[NSObject cancelPreviousPerformRequestsWithTarget:self];//可以成功取消全部。  
[[self class] cancelPreviousPerformRequestsWithTarget:self];//可以成功取消全部。  

代码下载地址:http://download.csdn.net/detail/samuelltk/5481327



阅读更多
个人分类: 经典错误
上一篇如何定位Obj-C野指针随机Crash(二):让非必现Crash变成必现
下一篇是否遵从某个协议 ios
想对作者说点什么? 我来说一句

TestForCancelPrePerformReq.zip

2013年05月30日 36KB 下载

没有更多推荐了,返回首页

关闭
关闭