- @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