我正在开发一个倒计时器,但在计数小于 0 时无法使用 if 语句来停止计时器。任何有关解决此问题的指导将不胜感激。预先感谢您的帮助..
-(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 =@"Times Up!";
NSLog(@"Times Up!");
// Timer gets killed and no longer calls updateInterface
[myTimer invalidate];
}
}
最佳答案
我测试了你的代码,它工作得很好,计时器停在-1。所以我最好的猜测是 time
可能被声明为无符号值,因此它永远不会小于零。
关于iphone - NSTimer - if 语句在计时器内不起作用 - 倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4174568/