Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在计时器xcode中显示前导零_Xcode_Nstimer - Fatal编程技术网

在计时器xcode中显示前导零

在计时器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

我想在我的应用程序中显示一个倒计时。计时器工作正常,但我希望它显示时间为00:00:00。一位数整数的前导零通常不显示(例如0:1:15),但我不知道如何在不使用笨重的if语句的情况下将它们输入。以下是相关代码:

-(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];