【发布时间】:2013-03-20 23:04:09
【问题描述】:
这个问题已经在堆栈溢出时被问死了,我已经看到了很多答案,但不知何故我仍然遇到了麻烦。
无论如何,我分配了一个 NSTimer,因为在我看来确实加载了:
NSTimer *oneSecondTicker = [[NSTimer alloc] init];
oneSecondTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabelsWithOneSecondTicker) userInfo:nil repeats:YES];
self.dateForTimeLabel = [NSDate date];
选择器的方法是:
-(void) updateLabelsWithOneSecondTicker {
if(self.dateForTimeLabel != nil)
{
self.lblTime.text = [NSString stringWithFormat:@"%f", fabs([self.dateForTimeLabel timeIntervalSinceNow])];
}
}
这种方法基本上每秒更新一个标签,给我一个计时器/秒表之类的东西。
我还有一个开始/暂停按钮,当按下它来暂停时:
[oneSecondTicker invalidate];
oneSecondTicker = nil;
如果按下按钮再次启动,方法是:
NSTimer *oneSecondTicker = [[NSTimer alloc] init];
oneSecondTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabelsWithOneSecondTicker) userInfo:nil repeats:YES];
self.dateForTimeLabel = [NSDate date];
我没有给定时器调用retain,它没有属性,也没有被合成。然而,它在接口中被声明。
为了解决这个问题,每次按下暂停/开始按钮都会发生什么,计时器不会失效,并且时间标签会越来越快地更新,这让我相信出于某种原因存在多个计时器。 (显然,在这些方法中还没有真正的计时器功能,这只是让我的 NSTimer 工作的测试)。
这怎么可能?
【问题讨论】: