【发布时间】:2016-06-01 16:08:56
【问题描述】:
一个奇怪的情况:
如果我一次又一次地启动我的计时器而不先停止它,它的计数会越来越快。我猜是因为它现在启动了多个计时器?
然而,当我终于想阻止它时,它无法阻止……永远继续下去。
(也许出于设计考虑,我应该禁止用户再次按开始,但我想知道这背后的真正原因是什么以及为什么无法停止计时器。)
- (IBAction)Start:(id)sender {
countInt = 0;
self.Time.text = [NSString stringWithFormat:@"%i", countInt];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countTimer) userInfo:nil repeats:YES];
}
- (IBAction)Stop:(id)sender {
[timer invalidate];
}
- (void) countTimer {
countInt += 1;
self.Time.text = [NSString stringWithFormat:@"%i", countInt];
}
@end
【问题讨论】:
-
显示你的代码,大概你没有失效,你也杀死了你对计时器的引用......
-
贴代码,不是图片! - 编辑您的问题。
-
@Wain 感谢您的快速回复!我添加了一个屏幕截图......不确定它是否是正确的附加方式。我确实使计时器无效。
-
@vadian 谢谢!我只是想出了如何发布代码...
标签: ios objective-c nstimer