- (NSString *)formattedCountdownTime {
//Get the time left until the specified date
NSInteger ti = ((NSInteger)[self.countdownStartTime timeIntervalSinceNow]);
NSInteger seconds = ti % 60;
NSInteger minutes = (ti / 60) % 60;
NSInteger hours = (ti / 3600) % 24;
NSInteger days = (ti / 86400);
//Update the lable with the remaining time
return [NSString stringWithFormat:@"%02li days %02li hrs %02li min %02li sec", (long)days, (long)hours, (long)minutes, (long)seconds];
}
答案 0 :(得分:1)
正如ChrisH所说,只需引用计时器并使用以下代码在 updateTime 中添加条件:
if (ti <= 0) [self.timer invalidate];
我无法支持ChrisH的回答,因为我没有50个声誉
修改强>
首先引用您的计时器
@interface ViewController ()
@property UIDatePicker *datePicker;
@property NSTimer *timer;
@end
在 startCountdown 更改中
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
与
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
updateTime 中的在您的计算之后添加此代码
if (ti <= 0) [self.timer invalidate];
Voilà:)