gpt4 book ai didi

iphone - NSTimer - if 语句在计时器内不起作用 - 倒计时器

转载 作者:行者123 更新时间:2023-12-03 18:03:31 25 4
gpt4 key购买 nike

我正在开发一个倒计时器,但在计数小于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com