3

所以我想做的是根据一天中的时间改变图像的背景,但它遇到了问题。这是代码:

-(void)updateBackground
{
    NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm a"];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *resultTime = [dateFormatter stringFromDate:currentTime];
    [timeLabel setText:resultTime];
    NSString *morningStart = [NSString stringWithFormat:@"05:00 AM"];
    NSString *morningEnd = [NSString stringWithFormat:@"11:59 AM"];
    NSString *afternoonStart = [NSString stringWithFormat:@"12:00 PM"];
    NSString *afternoonEnd = [NSString stringWithFormat:@"02:59 PM"];
    NSString *eveningStart = [NSString stringWithFormat:@"03:00 PM"];
    NSString *eveningEnd = [NSString stringWithFormat:@"07:59 PM"];
    NSString *nightStart = [NSString stringWithFormat:@"08:00 PM"];
    NSString *nightEnd = [NSString stringWithFormat:@"04:59 AM"];
    NSLog(resultTime);
    if ([resultTime compare:morningStart]==NSOrderedDescending) {
        if ([resultTime compare:morningEnd]==NSOrderedAscending) {
            UIImage *morning = [UIImage imageNamed:@"MorningTime.jpeg"];
            [background setImage:morning];
        }
    }
    if ([resultTime compare:afternoonStart]==NSOrderedDescending) {
        if ([resultTime compare:afternoonEnd]==NSOrderedAscending) {
            UIImage *afternoon = [UIImage imageNamed:@"afternoonSky.jpeg"];
            [background setImage:afternoon];
        }
    }
    if ([resultTime compare:eveningStart]==NSOrderedDescending) {
        if ([resultTime compare:eveningEnd]==NSOrderedAscending) {
            UIImage *evening = [UIImage imageNamed:@"EveningSky.jpeg"];
            [background setImage:evening];
        }
    }
    if ([resultTime compare:nightStart]==NSOrderedDescending) {
        if ([resultTime compare:nightEnd]==NSOrderedAscending) {
            UIImage *night = [UIImage imageNamed:@"NightSky.jpeg"];
            [background setImage:night];
        }
    }


}

那么问题出在哪里,假设现在是晚上 11:54,但它一直说是在上午 5:00 之后和上午 11:59 之前!这不是选择正确的 if 语句!我该怎么办?

4

3 回答 3

0

NSDate 格式以 24 小时格式提供时间,因此您必须更改时间(例如(01:00 AM,13:00 PM))以放置任何您想要的条件。

于 2012-10-24T05:10:03.153 回答
0

可能这个对你有帮助,

NSDate *currentTime = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm a"];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *resultTime = [dateFormatter stringFromDate:currentTime];
    NSString *morningStart = [NSString stringWithFormat:@"05:00 AM"];
    NSString *morningEnd = [NSString stringWithFormat:@"11:59 AM"];
    NSString *afternoonStart = [NSString stringWithFormat:@"12:00 PM"];
    NSString *afternoonEnd = [NSString stringWithFormat:@"14:59 PM"];
    NSString *eveningStart = [NSString stringWithFormat:@"15:00 PM"];
    NSString *eveningEnd = [NSString stringWithFormat:@"19:59 PM"];
    if ([morningStart compare:resultTime]==NSOrderedAscending & [resultTime compare:morningEnd]==NSOrderedAscending) {
            NSLog(@"morning");      
    }
   else  if ([afternoonStart compare:resultTime]==NSOrderedAscending & [resultTime compare:afternoonEnd]==NSOrderedAscending) {
                NSLog(@"aftrnon..."); 
            }
   else  if ([eveningStart compare:resultTime]==NSOrderedAscending & [resultTime compare:eveningEnd]==NSOrderedAscending) {
                NSLog(@"evening");    
    }
    else {
        NSLog(@"nit time");
    }
于 2012-10-24T05:13:52.510 回答
0

您应该使用NSDateComponents仅获取您正在比较的NSDate对象的小时和分钟。查看NSDateComponents的官方文档了解如何使用它。

然后使用以下方法之一比较NSDate对象:

- (NSDate *)earlierDate:(NSDate *)anotherDate
- (NSDate *)laterDate:(NSDate *)anotherDate
- (BOOL)isEqualToDate:(NSDate *)anotherDate

你不应该在比较日期时使用 NSString。

于 2012-10-25T06:24:36.317 回答