在计时器xcode中显示前导零
我想在我的应用程序中显示一个倒计时。计时器工作正常,但我希望它显示时间为00:00:00。一位数整数的前导零通常不显示(例如0:1:15),但我不知道如何在不使用笨重的if语句的情况下将它们输入。以下是相关代码:在计时器xcode中显示前导零,xcode,nstimer,Xcode,Nstimer,我想在我的应用程序中显示一个倒计时。计时器工作正常,但我希望它显示时间为00:00:00。一位数整数的前导零通常不显示(例如0:1:15),但我不知道如何在不使用笨重的if语句的情况下将它们输入。以下是相关代码: -(void)timerFireMethod: (NSTimer *)timer { [self setUpTimer]; self.timeLabel.text = [NSString stringWithFormat:@"%lu:%lu:%lu", self.time
-(void)timerFireMethod: (NSTimer *)timer {
[self setUpTimer];
self.timeLabel.text = [NSString stringWithFormat:@"%lu:%lu:%lu", self.timerParts.hour, self.timerParts.minute, self.timerParts.second];
self.counter--; }
有人知道我能做些什么来显示前导零,使0:1:15变成00:01:15吗?您可以通过添加
.2
强制显示前导零如果小于10且前导零,则始终显示两位数字
self.timeLabel.text = [NSString stringWithFormat:@"%.2lu:%.2lu:%.2lu", self.timerParts.hour, self.timerParts.minute, self.timerParts.second];