9

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!

user298261
  • 422
  • 3
  • 17

2 Answers2

19
int seconds = time % 60;
int minutes = (time - seconds) / 60;
theTimer.text = [NSString stringWithFormat:@"%d:%.2d", minutes, seconds];
Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
0

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]];
S. Matsepura
  • 1,673
  • 1
  • 17
  • 24