所以我想做的是根据一天中的时间改变图像的背景,但它遇到了问题。这是代码:
-(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 语句!我该怎么办?