【问题标题】:Switching ON and OFF the Alarm ios打开和关闭警报 ios
【发布时间】:2012-09-19 03:53:45
【问题描述】:
我已经准备了一个闹钟应用程序,它使用UILocalnotification 来安排闹钟。现在设置闹钟后,我想做一个开关,以便我可以使用UISwitch 打开和关闭它。我只是不知道我该怎么做?我现在的想法是,当您关闭警报时,我将在取消 UILocalnotification 之前存储 DATE 和 TIME 值,以便当用户再次打开警报时我重新安排它与存储的日期和时间值。这是正确的做法还是有其他方法可以做到这一点?
【问题讨论】:
标签:
ios
xcode
uilocalnotification
alarm
【解决方案1】:
只需创建具有“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];