NSTimer - 如果语句不在定时器内工作 - CountDown定时器
问题描述:
我正在倒计时计时器,并且无法在计数小于0时使if语句停止定时器。解决此问题的任何指导将会很大赞赏。在此先感谢您的帮助..NSTimer - 如果语句不在定时器内工作 - CountDown定时器
-(void) startCountdown{
time = 90;
//NSLog[@"Time Left %d Seconds", time];
//This timer will call the function updateInterface every 1 second
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateInterface:) userInfo:nil repeats:YES];
}
-(void) updateInterface:(NSTimer*)theTimer{
if(time >= 0){
time --;
CountDownText.text = [NSString stringWithFormat:@"%d", time];
NSLog(@"Time Left %d Seconds", time);
}
else{
CountDownText.text [email protected]"Times Up!";
NSLog(@"Times Up!");
// Timer gets killed and no longer calls updateInterface
[myTimer invalidate];
}
}
答
看起来你的倒计时不会停止,直到它得到,因为你检查大于 - 或 - 等于来为-1(而不是0)零然后递减(然后显示)。
要么减量,然后再检查,如果时间是大于零:
-(void) updateInterface:(NSTimer*)theTimer{
time --;
if(time > 0){
CountDownText.text = ...
或检查,如果时间大于1:
-(void) updateInterface:(NSTimer*)theTimer{
if(time > 1){
time --;
CountDownText.text = ...
答
我测试你的代码,它完美地工作并且定时器停在-1。所以我最好的猜测是time
可能被声明为无符号值,所以它永远不会小于零。
+0
以前,计时器将运行到负数,并不会停止。我将时间值设置为> = 1,现在停止计数为0.感谢所有支持大家 – 2010-11-14 18:21:42
问题是什么?计时器是否继续倒计时? – 2010-11-13 20:41:11