当日期倒计时完成时更改 UILabel

Change UILabel when countdown to a date completes

我的应用程序中有一个倒计时,我需要在倒计时到达日期时将文本更改为“完成”。

这是代码:

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;

我找到了答案

这里是:

)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;
}

根据您的解释,我了解到您只需要检查 "current date" 是否晚于 "endingDate"。

您可以替换:

gamesCountdownLabel.text = countdownText;

与:

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

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