倒数计时器在iOS 6上不起作用

时间:2013-12-18 14:35:34

标签: ios uilabel nsdate nstimer countdown

我的应用中有倒数计时器。它会计算某天到达之前的天,小时,分钟和秒。我用下面列出的代码完成了这个。视图中共有9个标签。 4显示剩余的小时数,以及紧接在每个标记下方的4小时,将上述每个标签标记为天,小时,分钟和秒。第9个标签是覆盖所有其他人的标签,并且应该保持隐藏直到白天到来,并且它说它是时间开始。这是我的代码。

-(void) viewWillAppear:(BOOL)animated
  [self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];

}
- (void)updateCounter:(NSTimer *)theTimer {
    if(secondsLeft > 0 ){
        secondsLeft -- ;
        days = secondsLeft / 86400;
        NSLog(@"Notgreaterthan0%d", days);
        hours = secondsLeft / 3600;
        minutes = (secondsLeft % 3600) / 60;
        seconds = (secondsLeft %3600) % 60;
        dayslabel.text = [NSString stringWithFormat:@"%02d", days];
        hourslabel.text = [NSString stringWithFormat:@"%02d", hours];
        minuteslabel.text = [NSString stringWithFormat:@"%02d", minutes];
        secondslabel.text = [NSString stringWithFormat:@"%02d", seconds];
        beginLabel.hidden = YES;
    }
    else{
        NSLog(@"BEGIN");
        //myCounterLabel.text = @"It's Time To Begin";
        beginLabel.hidden = NO;
        beginLabel.text = @"It's Time To Begin!";
    }
}

- (void)updateCountdown {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSLog(@"updateCountdownmethod");
    NSDate *endingDate = [dateFormatter dateFromString:@"2013-12-27"];
    NSDate *startingDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0];

    NSInteger days     = [dateComponents day];
    NSInteger hours    = [dateComponents hour];
    NSInteger minutes  = [dateComponents minute];
    NSInteger seconds  = [dateComponents second];
    NSLog(@"TESTTESTTEST%ld%ld%ld%ld",(long)days, (long)minutes, (long)seconds, (long)hours);
    if (days > 0 | hours > 0 | minutes > 0 | seconds > 0 ) {
        dayslabel.text = [NSString stringWithFormat:@"%02d", days];
        hourslabel.text = [NSString stringWithFormat:@"%02d", hours];
        minuteslabel.text = [NSString stringWithFormat:@"%02d", minutes];
        secondslabel.text = [NSString stringWithFormat:@"%02d", seconds];
        itsaysdays.hidden = NO;
        itsayshours.hidden = NO;
        itsaysminutes.hidden = NO;
        itsaysseconds.hidden = NO;

        beginLabel.hidden = YES;
        [self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1];
    }
    else {
        dayslabel.hidden = YES;
        hourslabel.hidden = YES;
        minuteslabel.hidden = YES;
        secondslabel.hidden = YES;
        beginLabel.hidden = NO;
        itsaysdays.hidden = YES;
        itsayshours.hidden = YES;
        itsaysminutes.hidden = YES;
        itsaysseconds.hidden = YES;
        beginLabel.text = @"It's time to begin!";

    }

}

这一切在iOS 7上运行良好,但在任何较低的情况下,它会立即显示It的开始时间标签,以掩盖其余部分。我的TESTESTEST日志(希望它引人注目)回来了

22-28-3-8

所以我知道它正确地得到了时间,但由于某种原因忽略了if语句,它应该保持一切正确隐藏。

更新:我已经尝试在fromDate toDate部分代码中切换日期,但它仍然会混乱。出于某种原因,它只是在iOS 6上用完全错误的NSDateComponents回来了

0 个答案:

没有答案