2012-09-19 92 views
5

我准备了一个闹钟应用程序,它使用UILocalnotification来安排闹钟。现在闹钟设置完成后,我想要一个开关,以便我可以使用UISwitch将其打开和关闭。我只是不能图我该怎么办呢?我在想什么截至现在,当你关闭闹钟,我将取消UILocalnotification,这样当用户在报警我再次切换之前存储日期和时间值与存储的日期和时间值重新计划。它是做正确的方式或有任何其他的方式来做到这一点?打开和关闭报警ios

回答

7

只是让它们具有“日期”,“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];