【问题标题】:Change UILabel when countdown to a date completes [closed]日期倒计时完成时更改 UILabel [关闭]
【发布时间】:2015-04-02 01:42:57
【问题描述】:

我的应用中有倒计时,当倒计时到达日期时,我需要将文本更改为 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;

【问题讨论】:

  • 只检查是否所有组件都为零...我不明白你的问题。
  • @JoshCaswell 如果您看到我附上的图片,您会发现倒计时 - 4 天。当点击数据库中标签更改为发布的日期时,我需要倒计时

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


【解决方案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;
}

【讨论】:

    【解决方案2】:

    根据您的解释,我了解到您只需要检查“当前日期”是否晚于“结束日期”即可。

    你可以替换:

    gamesCountdownLabel.text = countdownText;
    

    与:

    NSDate *refDate = [NSDate date]; // current date as a reference time
    
    gamesCountdownLabel.text = ([refDate isEqualToDate: [endingDate laterDate: refDate]) ? “FINSIH” : countdownText;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-04
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      相关资源
      最近更新 更多