所以我要做的是根据一天中的时间更改图像的背景,但它遇到了问题。这是代码:
-(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点之后和上午11:59之前!这不是选择正确的if语句!我该怎么办?
答案 0 :(得分:0)
NSDate格式以24小时格式提供时间,因此您必须更改时间(上午01:00,下午13:00),以便根据需要设置条件。
答案 1 :(得分: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");
}
答案 2 :(得分:0)
您应该使用 NSDateComponents 来获取您正在比较的 NSDate 对象的小时和分钟。请参阅 NSDateComponents 的官方文档,了解如何使用它。
然后使用以下方法之一比较 NSDate 对象:
- (NSDate *)earlierDate:(NSDate *)anotherDate
- (NSDate *)laterDate:(NSDate *)anotherDate
- (BOOL)isEqualToDate:(NSDate *)anotherDate
比较日期时,不应使用NSString。