这是我的代码。我希望计时器在启动后5秒内停止,但不启动。这里有什么问题?NSTimer代码永远执行
-(void)loadView
{
NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0
target:self
selector:@selector(targetMethod:)
userInfo:nil
repeats:YES];
if([NSDate timeIntervalSinceReferenceDate] - startTime >= 5) {
[timer invalidate];
}
}
-(void)targetMethod:(NSTimer *)timer {
NSLog(@"bla");
}
你似乎误会NSTimer'如何'的作品,因为在'targetMethod代码:'没有任何意义。正在尝试做什么? – Costique 2012-02-04 12:20:50
我忘了添加targetMethod。请参阅更新的版本。 – objlv 2012-02-04 12:22:48