时间00:00:00:00如何停止倒计时?

时间:2015-12-11 19:23:20

标签: ios objective-c cocoa-touch

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

1 个答案:

答案 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à:)