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

标签 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 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/

相关文章:

ios - 非平凡循环中的 block

objective-c - UITableView:如何删除索引?

objective-c - 子类不继承其功能

cocoa - 截断标签的开头

iphone - 在自定义 UITableView 中显示空白 UITableViewCell

iphone - 尝试取消本地通知时应用程序抛出 'NSInvalidArgumentException'

iphone - 如果我将我的应用程序部署目标设置为 5.1,它是否与 iOS 7.0 版本兼容

iphone - 在 iPhone 游戏中播放声音 - 速度变慢

objective-c - 缓存 NSView 上下文

objective-c - 如何在 Objective-C 中的同一位置重命名或更改文件名大小写