【发布时间】:2013-07-30 18:08:34
【问题描述】:
试图从给定的 NSTimeInterval 中创建倒数计时器,标签似乎没有更新。
- (IBAction)startTimer:(id)sender{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (void)timerAction:(NSTimer *)t {
if(testTask.timeInterval == 0){
if (self.timer){
[self timerExpired];
[self.timer invalidate];
self.timer = nil;
}
else {
testTask.timeInterval--;
}
}
NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds / 3600, (seconds / 60) % 60, seconds % 60];
timerLabel.text = string;
}
【问题讨论】:
-
testTask 由用户指定,我一直在以 10 秒的时间间隔测试它。它显示了 10 秒,但没有显示任何变化。
-
而且时间间隔实际上从未减少
-
你是否在 timerAction 方法中设置了断点?它真的被调用了吗?
-
刚试过。是的,它被调用了
-
我会检查或 NSLog 秒值和字符串值。您还应该在 else 部分中添加一个断点,以查看是否正在发生递减。
标签: iphone ios objective-c nstimer countdown