iPhone:nstimer倒计时(显示分钟:秒)
题
我已经设置了计时器代码,这全是犹太洁食,但是我希望我的标签显示“分钟:秒”,而不仅仅是几秒钟。
-(void)countDown{
time -= 1;
theTimer.text = [NSString stringWithFormat:@"%i", time];
if(time == 0)
{
[countDownTimer invalidate];
}
}
我已经将“时间”设置为600或10分钟。但是,我希望显示器显示10:59、10:58等,直到达到零为止。
我该怎么做呢?
谢谢!
解决方案
int seconds = time % 60;
int minutes = (time - seconds) / 60;
theTimer.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds];
其他提示
到了我的第一个答案时,我已经升级了一些,它消除了与之相关的冲突 NSInteger
和 int
:
NSInteger secondsLeft = 987;
int second = (int)secondsLeft % 60;
int minutes = ((int)secondsLeft - second) / 60;
theTimer.text = [NSString stringWithFormat:@"%02d:%02d", minutes, second]];
不隶属于 StackOverflow