Question

Hello I'm trying to make a simple countdown app.

However my countdown is only in seconds and not in minutes and hours. How do I make it such?

Was it helpful?

Solution 2

Create the timer:

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(Countdown) userInfo:nil repeats: YES];

Countdown method:

-(void) Countdown {
    int hours, minutes, seconds;

    secondsLeft--;
    hours = secondsLeft / 3600;
    minutes = (secondsLeft % 3600) / 60;
    seconds = (secondsLeft %3600) % 60;
    label.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}

Reference : https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html

OTHER TIPS

You need to use some logic, to convert seconds to minutes, hours.

Lets say :

s Seconds.

h = s/3600;
m = s/60-h*60;
s = s%60;
NSString *clockTime = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",h,m,s];

Then you can form a string with your desired hh:mm:ss

Here is your Answer:-

- (void)displayLabel:(UILabel *)lbl timerInterval:(NSTimeInterval)timerInterval {
 uint seconds = fabs(timerInterval); 
 uint minutes = seconds / 60;
 uint hours = minutes / 60;
 seconds -= minutes * 60;
 minutes -= hours * 60;

 lbl.text=[NSString stringWithFormat:@"%@%02uh:%02um:%02us", (timerInterval<0?@"-":@""),         hours, minutes, seconds];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top