【问题标题】:Xcode - setting limit for NSDate CountDown timerXcode - 设置 NSDate 倒计时计时器的限制
【发布时间】:2012-04-26 12:17:53
【问题描述】:

我正在使用 xCode 4.3.2,在我的倒计时计时器项目(代码如下所示)中,我想在 30 秒后停止倒计时。如何为我的倒计时设置此限制? 你能帮帮我吗?

int countLimit=30; //seconds
NSDate *startDate;

- (void)countDown{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = -([currentDate timeIntervalSinceDate:startDate]);
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    self.myCounterLabel.text = timeString;
    [dateFormatter release];
}

- (void)updateCounter{

    self.myCounterLabel.text = @"00:00:00";
    startDate = [[NSDate date]retain];

    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
    target:self
    selector:@selector(countDown)
    userInfo:nil
    repeats:YES];
}

【问题讨论】:

  • 我无法让这段代码工作,但here's a great tutorial 了解如何制作一个非常适合我的倒数计时器。

标签: objective-c xcode nsdate nstimer nstimeinterval


【解决方案1】:

只需添加到您的countDown

if([[NSDate date] timeIntervalSinceDate:startDate]>=countLimit)

[myTimer invalidate];

【讨论】:

  • 它给了我两个警告:- 1. 类方法'+timeIntervalSinceDate:' 未找到(返回类型默认为'id')。 2. 指针与整数的有序比较('id' and 'int')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-15
相关资源
最近更新 更多