2012-09-19 93 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];