Reputation: 1185
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?
Upvotes: 0
Views: 1703
Reputation: 46543
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
Upvotes: 1
Reputation: 9246
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];
}
Upvotes: 0
Reputation: 461
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
Upvotes: 0