views:

400

answers:

1

Hello!

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!

+1  A: 
int seconds = time % 60;
int minutes = (time - seconds) / 60;
theTimer.text = [NSString stringWithFormat:@"%i:%i", minutes, seconds];
Noah Witherspoon
That's great! However, once the seconds gets below ten it shows "9:9", "9:8", etc.Is there a way to make that "9:09", "9:08"?
Sure. `NSString *padZero = @""; if(seconds < 10) padZero = @"0"; theTimer.text = [NSString stringWithFormat:@"%i:%@%i",minutes,padZero,seconds];`
Noah Witherspoon
I'd rather use printf formatting for padding numbers like that: `[NSString stringWithFormat:@"%d:%.2d", minutes, seconds]`
Daniel Bauke
Awesome! You guys are brilliant! Thanks a bunch! :D