今天的日期是2013-04-17 10:39:00 +0000。现在我从表格中选择工作日来设定当天的时间表(闹钟)。我想永远在选定的日子显示本地通知提醒。但我遇到了问题。
这是我的代码:
-(IBAction)setDays{
NSDate *date1=[df dateFromString:time123]; //2013-04-17 10:39:00 +0000
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:date1];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:date1];
[components setHour:[timeComponents hour]];
[components setMinute:[timeComponents minute]];
[components setSecond:0];
NSString *su=@"false";
NSString *mo=@"false";
NSString *tu=@"false";
NSString *we=@"false";
NSString *th=@"false";
NSString *fr=@"false";
NSString *sa=@"false";
if ([[selectedDays objectAtIndex:0] isEqualToString:@"1"]) {
su=@"true";
[components setWeekday:1];
NSDate *itemDate = [calendar dateFromComponents:components];
NSLog(@"date1 %@",itemDate);
[appDelegate scheduleNotification:@"hi shiv" repeatDaily:YES fireDate:itemDate forKey:@"1"];
}
if ([[selectedDays objectAtIndex:1] isEqualToString:@"1"]) {
mo=@"true";
[components setWeekday:2];
NSDate *itemDate = [calendar dateFromComponents:components];
NSLog(@"date2 %@",itemDate);
[appDelegate scheduleNotification:@"hi shiv" repeatDaily:YES fireDate:itemDate forKey:@"2"];
}
if ([[selectedDays objectAtIndex:2] isEqualToString:@"1"]) {
tu=@"true";
[components setWeekday:3];
NSDate *itemDate = [calendar dateFromComponents:components];
NSLog(@"date3 %@",itemDate);
[appDelegate scheduleNotification:@"hi shiv" repeatDaily:YES fireDate:itemDate forKey:@"3"];
}
if ([[selectedDays objectAtIndex:3] isEqualToString:@"1"]) {
we=@"true";
[components setWeekday:4];
NSDate *itemDate = [calendar dateFromComponents:components];
NSLog(@"date4 %@",itemDate);
[appDelegate scheduleNotification:@"hi shiv" repeatDaily:YES fireDate:itemDate forKey:@"4"];
}
if ([[selectedDays objectAtIndex:4] isEqualToString:@"1"]) {
th=@"true";
[components setWeekday:5];
NSDate *itemDate = [calendar dateFromComponents:components];
NSLog(@"date5 %@",itemDate);
[appDelegate scheduleNotification:@"hi shiv" repeatDaily:YES fireDate:itemDate forKey:@"5"];
}
if ([[selectedDays objectAtIndex:5] isEqualToString:@"1"]) {
fr=@"true";
[components setWeekday:6];
NSDate *itemDate = [calendar dateFromComponents:components];
NSLog(@"date6 %@",itemDate);
[appDelegate scheduleNotification:@"hi shiv" repeatDaily:YES fireDate:itemDate forKey:@"6"];
}
if ([[selectedDays objectAtIndex:6] isEqualToString:@"1"]) {
sa=@"true";
[components setWeekday:7];
NSDate *itemDate = [calendar dateFromComponents:components];
NSLog(@"date7 %@",itemDate);
[appDelegate scheduleNotification:@"hi shiv" repeatDaily:YES fireDate:itemDate forKey:@"7"];
}
}
在AppDelegate.m中
-(UILocalNotification * )scheduleNotification:(NSString *)remainderText repeatDaily:(BOOL )isRepeat fireDate:(NSDate *)fireDate forKey:(NSString*)key{
Class cls = NSClassFromString(@"UILocalNotification");
UIApplication *app = [UIApplication sharedApplication];
if (cls != nil) {
NSLog(@"in set local notification");
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = fireDate;
if (isRepeat == FALSE) {
notif.repeatInterval = 0;
} else {
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSWeekCalendarUnit;
}
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = remainderText;
notif.alertAction = @"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"shiv",key, nil];
[app scheduleLocalNotification:notif];
NSLog(@"Added an event with ");
return notif;
}
return nil;
}