问题
So what I'm trying to do is change the background of an image according to the time of day, but it's running into a problem. Here's the code:
-(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];
}
}
}
So what the problem is, let's say it's 11:54 PM, but it keeps saying that it's after 5:00 AM and before 11:59 AM! It's not choosing the right if statement! What do I do?
回答1:
may be this one helps you,
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:
NSDate format gives the time in 24 hours format so you have to change the time like (01:00 AM,13:00 PM )for placing conditions whatever you want.
回答3:
You should use the NSDateComponents to get only the hours and minutes of the NSDate objects that you are comparing. See the official documentation of NSDateComponents to figure out how to use it.
Then you compare the NSDate objects using one of the following methods:
- (NSDate *)earlierDate:(NSDate *)anotherDate
- (NSDate *)laterDate:(NSDate *)anotherDate
- (BOOL)isEqualToDate:(NSDate *)anotherDate
You should never use NSString when comparing dates.
来源:https://stackoverflow.com/questions/12995103/changing-background-according-to-time-objective-c