我有一个应用程序每小时触发一次 UILocalNotifications,它将在用户在日期选择器中选择的时间停止。我通过将小时分量加一来预先设置 23 个本地通知。问题是,当凌晨 12:00 小时过去时,这一天仍然在同一天,不会改变。当时间到达凌晨 12:00 时,如何让 NSDate 更改日期?谢谢您的帮助。
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];
//date chosen from the picker with seconds = 0 i.e., final alarm
NSDate *itemDate = [calendar dateFromComponents:dateComps];
//setting the final alarm
UILocalNotification *finalAlarm = [[UILocalNotification alloc] init];
if (finalAlarm) {
finalAlarm.fireDate = itemDate;
finalAlarm.timeZone = [NSTimeZone defaultTimeZone];
finalAlarm.repeatInterval = 0;
finalAlarm.soundName = UILocalNotificationDefaultSoundName;
finalAlarm.alertBody = @"Test message...";
[[UIApplication sharedApplication] scheduleLocalNotification:finalAlarm];
}
//Formatting original date to isolate the current hour
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];
//Formatting original date to isolate the current minute
NSDateFormatter *minuteFormatter = [[NSDateFormatter alloc]init];
[minuteFormatter setDateFormat:@"mm"];
//creating a NSDateFormatter for readability
NSDateFormatter *yearMonthDay = [[NSDateFormatter alloc]init];
[yearMonthDay setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
//isolate current date/hour/minute
NSDate *currentDate = [NSDate date];
NSString *currentHour = [dateFormatter stringFromDate:currentDate];
NSString *currentMinute = [minuteFormatter stringFromDate:currentDate];
NSString *currentForamttedDate = [yearMonthDay stringFromDate:currentDate];
NSLog(@"Current Date: %@",currentForamttedDate);
//current hour int
int currentHourInt = [currentHour intValue];
//current minute int
int currentMinuteInt = [currentMinute intValue];
//current hour plus 1
int firstAlarmHour = currentHourInt + 1;
//creating the first alarm
NSDateComponents *firstAlarm = [[NSDateComponents alloc]init];
[firstAlarm setDay:[dateComponents day]];
[firstAlarm setMonth:[dateComponents month]];
[firstAlarm setYear:[dateComponents year]];
[firstAlarm setHour:firstAlarmHour];
[firstAlarm setMinute:currentMinuteInt];
[firstAlarm setSecond:0];
//creating a date from the components
NSDate *firstAlarmDate = [calendar dateFromComponents:firstAlarm];
//setting the first alarm
[self scheduleNotificationForDate: firstAlarmDate finalAlarm:itemDate];
-(void) scheduleNotificationForDate: (NSDate*)date finalAlarm: (NSDate *)finalAlarmDate {
NSComparisonResult result = [finalAlarmDate compare:date];
if (alarm) {
alarm.fireDate = date;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.alertBody = @"Test message...";
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
}