我准备了一个闹钟应用程序,它使用UILocalnotification来安排闹钟.现在闹钟设置后,我想做一个开关,这样我就可以用UISwitch打开和关闭它.我只是想不知道怎么办?我现在想的是,当你关闭闹钟时,我会在取消UILocalnotification之前存储DATE和TIME值,这样当用户再次打开闹钟时我会用存储的DATE和TIME值重新安排它.这是正确的做法,还是有其他方法可以做到这一点?
解决方法
只需使数据库表具有’date’,’isCanceled’字段和唯一id’alarmId’列(使用任何你想要的休息).所以当用户想要取消闹钟时试试这个,
NSString *alarmId = @"some_id_to_cancel"; UILocalNotification *notificationToCancel=nil; for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) { if([aNotif.userInfo objectForKey:@"ID"] isEqualToString:alarmId]) { notificationToCancel = aNotif; break; } } [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
为了更好地使用它,你可以通过
UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = itemDate; localNotif.timeZone = [NSTimeZone defaultTimeZone]; localNotif.alertAction = NSLocalizedString(@"View Details",nil); localNotif.alertBody = title; localNotif.soundName = UILocalNotificationDefaultSoundName; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"ID"]; localNotif.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif release];