3

我有一个 NSTimeInterval 基本上是

NSTimeInterval interval = [date1 timeIntervalSinceDate: [NSDate date]];

当我在视图 X 中时,我希望这个计算始终在后台运行,所以我可以在 UILabel 中显示一个倒计时的计时器。我该怎么做?就像在groupon iphone app中一样,但它并没有以秒为单位显示细节

4

1 回答 1

1

您可以使用NSTimer来获取以设定的时间间隔调用的方法(但是这一切都在主线程上执行,而不是在后台线程上执行):

- (void)setupTimer {
    //Start a timer to call update updateInterval: every 1 second
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                               target:self
                                               selector:@selector(updateInterval:)
                                             userInfo:nil
                                              repeats:YES];
}

- (void)updateInterval:(NSTimer *)timer {
    NSTimeInterval interval = [date1 timeIntervaleSinceDate:[NSDate date]];
    //Do other things like updating the label
}
于 2011-03-31T05:41:48.770 回答