我已经准备了一个闹钟应用程序,它使用UILocalnotification来安排在闹钟设置后的alarm.Now,我想做一个开关,这样我就可以使用UISwitch.I打开和关闭它,我不知道我怎么做?我现在的想法是,当你关闭闹钟,我应该存储日期和时间值之前取消UILocalnotification,以便当用户再次打开闹钟时,我重新安排它与存储的日期和时间值。这是正确的方法吗?或者有其他方法可以做到吗?
发布于 2012-09-19 12:16:47
只需创建包含'date‘、'isCanceled’字段和唯一id 'alarmId‘列的数据库表(您可以随意使用rest )。因此,当用户想要取消警报时,请尝试此操作,
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];https://stackoverflow.com/questions/12488301
复制相似问题