我准备了一个使用 UILocalnotification
的闹钟应用程序用于安排闹钟。设置闹钟后,我想做一个开关,以便我可以使用 UISwitch
打开和关闭它。 .我只是不知道我该怎么做?我现在想的是,当你关闭闹钟时,我将在取消 UILocalnotification
之前存储日期和时间值。这样当用户再次打开闹钟时,我会使用存储的 DATE 和 TIME 值重新安排它。这是正确的做法还是有其他方法可以做到这一点?
最佳答案
只需创建包含“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];
关于ios - 打开和关闭闹钟 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488301/