【问题标题】:iPhone: NSTimer Countdown (Display Minutes:Seconds)iPhone:NSTimer 倒计时(显示分钟:秒)
【发布时间】:2010-04-13 02:21:36
【问题描述】:

我设置了我的计时器代码,而且都是 kosher,但我希望我的标签显示“分钟:秒”而不是秒。

-(void)countDown{

    time -= 1;

    theTimer.text = [NSString stringWithFormat:@"%i", time];

    if(time == 0)
    {
    [countDownTimer invalidate];
    }
}

我已经将“时间”设置为 600 或 10 分钟。但是,我希望显示器显示 10:59、10:58 等,直到它达到零。

我该怎么做?

谢谢!

【问题讨论】:

    标签: iphone nstimer countdown


    【解决方案1】:
    int seconds = time % 60;
    int minutes = (time - seconds) / 60;
    theTimer.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds];
    

    【讨论】:

    • 太棒了!但是,一旦秒数低于 10,它会显示“9:9”、“9:8”等。有没有办法让“9:09”、“9:08”?
    • 当然。 NSString *padZero = @""; if(seconds < 10) padZero = @"0"; theTimer.text = [NSString stringWithFormat:@"%i:%@%i",minutes,padZero,seconds];
    • 我宁愿使用 printf 格式来填充这样的数字:[NSString stringWithFormat:@"%d:%.2d", minutes, seconds]
    • 太棒了!你们太棒了!非常感谢! :D
    • 大家如何声明时间。
    【解决方案2】:

    到那时,我对第一个答案进行了一些升级,它消除了与 NSIntegerint 相关的冲突:

    NSInteger secondsLeft = 987;
    int second = (int)secondsLeft % 60;
    int minutes = ((int)secondsLeft - second) / 60;
    theTimer.text = [NSString stringWithFormat:@"%02d:%02d", minutes, second]];
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2017-02-06
      相关资源
      最近更新 更多