使用 [ self performSelector: @selector (sel:) withObject: @"active" afterDelay: 3 ];执行一个延时消息。
当要取消的时候,可以使用:
[[self class] cancelPreviousPerformRequestsWithTarget: self ];
Demo:
#import "MyObject.h"
@implementation MyObject
- ( IBAction )myAction:( id )sender
{
[myOutlet setTitle: @"wait 3 second" ];
[ self performSelector: @selector (sel:) withObject: @"active" afterDelay: 3 ];
}
- ( void )sel:(NSString *)msg
{
[myOutlet setTitle:msg];
}
- ( IBAction )stop:( id )sender
{
[MyObject cancelPreviousPerformRequestsWithTarget: self ];
NSLog( @"stop" );
}
@end