我正在尝试在一个循环中添加UILocalNotification,其中包含不同的日期,例如一周。当我添加火日期不变。以下是我的代码,请帮我解决这个问题。提前谢谢。
NSDate *now = [NSDate date];
BOOL isTimer = NO;
for(i=1;i<8;i++){
NSDate *date = [now dateByAddingTimeInterval:60*60*24*i];
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; //Create the localNotification object
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
NSLog(@"user date time before firing %@",date);
[localNotification setFireDate:(isTimer?[NSDate getDateFromString:[NSDate getEndDateTime:timeInt]]:date)];
NSLog(@"notification fire date %@",localNotification.fireDate);
//Set the date when the alert will be launched using the date adding the time the user selected on the timer
[localNotification setAlertAction:@"Open"]; //The button's text that launches the application and is shown in the alert
[localNotification setAlertBody:titleHeader]; //Set the message in the notification from the textField's text
localNotification.soundName = UILocalNotificationDefaultSoundName;
[localNotification setHasAction: YES]; //Set that pushing the button will launch the application
[localNotification setUserInfo:[NSDictionary dictionaryWithKeysAndObjects:@"title",titleHeader,@"url",link, nil]];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification = nil;
}
以下是日志
user date time before firing 2015-06-10 05:22:00 +0000
notification fire date 2015-06-10 16:11:14 +0000
user date time before firing 2015-06-11 05:22:00 +0000
notification fire date 2015-06-10 16:11:19 +0000
user date time before firing 2015-06-12 05:22:00 +0000
notification fire date 2015-06-10 16:12:34 +0000
user date time before firing 2015-06-13 05:22:00 +0000
notification fire date 2015-06-10 16:12:38 +0000
user date time before firing 2015-06-14 05:22:00 +0000
notification fire date 2015-06-10 16:12:39 +0000
user date time before firing 2015-06-15 05:22:00 +0000
notification fire date 2015-06-10 16:12:39 +0000
user date time before firing 2015-06-16 05:22:00 +0000
notification fire date 2015-06-10 16:12:39 +0000
答案 0 :(得分:2)
问题是isTimer
是真的,所以你一遍又一遍地使用相同的日期;每次通过循环时,date
变量都不同,但从不使用它。