iPhone:NSTimer 倒计时(显示分钟:秒)

发布于 2024-08-28 06:23:54 字数 331 浏览 5 评论 0原文

我已经设置了计时器代码,而且一切都正常,但我希望我的标签显示“分钟:秒”而不是仅显示秒。

-(void)countDown{

    time -= 1;

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

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

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

我该怎么做?

谢谢!

I have my timer code set up, and it's all kosher, but I want my label to display "Minutes : seconds" instead of just seconds.

-(void)countDown{

    time -= 1;

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

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

I've already set "time" to 600, or 10 minutes. However, I want the display to show 10:59, 10:58, etc. until it reaches zero.

How do I do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-09-04 06:23:54
int seconds = time % 60;
int minutes = (time - seconds) / 60;
theTimer.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds];
int seconds = time % 60;
int minutes = (time - seconds) / 60;
theTimer.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds];
梅窗月明清似水 2024-09-04 06:23:54

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

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

By the time, I was made a little upgrade of first answer, it removed conflicts that associated from NSInteger and int:

NSInteger secondsLeft = 987;
int second = (int)secondsLeft % 60;
int minutes = ((int)secondsLeft - second) / 60;
theTimer.text = [NSString stringWithFormat:@"%02d:%02d", minutes, second]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文