倒计时到日期后更改UILabel

时间:2015-04-02 01:42:57

标签: ios objective-c cocoa-touch time uilabel

我的应用程序中有倒计时,当倒计时到达日期时我需要将文本更改为FINISH。

以下是代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSDate *startingDate = [NSDate date];
NSDate *endingDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@", currentGame.gameDate]];
NSUInteger unitFlags = NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitMinute|NSCalendarUnitSecond;

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *componentsDaysDiff = [gregorianCalendar components:unitFlags
                                                            fromDate:startingDate
                                                              toDate:endingDate
                                                             options:0];


NSString *countdownText = [NSString stringWithFormat:@"%ld Months %ld Days %ld Minutes %ld Seconds", (long)componentsDaysDiff.month, (long)componentsDaysDiff.day, (long)componentsDaysDiff.minute, (long)componentsDaysDiff.second];

gamesCountdownLabel.text = countdownText;

2 个答案:

答案 0 :(得分:1)

我找到了答案

这是:

)componentsDaysDiff.second];
if ((long)componentsDaysDiff.month <= 0 && (long)componentsDaysDiff.day <= 0 && (long)componentsDaysDiff.minute <= 0 && (long)componentsDaysDiff.second <= 0 ) {
    gamesCountdownLabel.text = @"Released";
}else{
gamesCountdownLabel.text = countdownText;
}

答案 1 :(得分:0)

根据您的解释,我了解您只需要检查&#34;当前日期&#34;晚于&#34; endingDate&#34;。

您可以替换:

gamesCountdownLabel.text = countdownText;

使用:

NSDate *refDate = [NSDate date]; // current date as a reference time

gamesCountdownLabel.text = ([refDate isEqualToDate: [endingDate laterDate: refDate]) ? “FINSIH” : countdownText;