Iphone NSTimer-if语句在计时器内不工作-倒计时计时器

Iphone NSTimer-if语句在计时器内不工作-倒计时计时器,iphone,objective-c,cocoa,nstimer,Iphone,Objective C,Cocoa,Nstimer,我正在使用倒计时,当计数小于0时,很难让if语句停止计时器。如有任何关于解决此问题的指导,将不胜感激。提前感谢你的帮助 -(void) startCountdown{ time = 90; //NSLog[@"Time Left %d Seconds", time]; //This timer will call the function updateInterface every 1 second myTimer = [NSTimer scheduledTimerWithTime

我正在使用倒计时,当计数小于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(而不是0)之前不会停止,因为您正在检查大于或-equal为零,然后递减(然后显示)

先减小,然后检查时间是否大于零:

-(void) updateInterface:(NSTimer*)theTimer{
    time --;
    if(time > 0){
        CountDownText.text = ...
或检查时间是否大于1:

-(void) updateInterface:(NSTimer*)theTimer{
    if(time > 1){
        time --;
        CountDownText.text = ...

我测试了你的代码,它工作得很好,计时器停在-1。所以我最好的猜测是,
time
可能被声明为无符号值,所以它永远不会小于零。

问题是什么?计时器是否继续倒计时?以前,计时器将运行到负数,并且不会停止。我将时间值设置为>=1,现在将计数停止在0。谢谢大家的支持