关于NSTimer,最近在使用的时候,发现一些有意思的东西,和大家分享一下,其实也满简单的,平时只要多注意一些就能够很好得使用它们。比如我们定义NSTimer的实例的时候,一般有两种方式:
#1:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:selfselector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
[timer fire];
#2:
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0 target:selfselector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
以上两种方式的区别在于第二种方式定义后需要手动得将NSTimer加入到当前NSRunLoop的循环中去,这个在timerWithTimeInterval的定义中有如下的说明:
“You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds seconds have elapsed, the timer fires, sending the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)Parameters”