2012-02-04 4 views
7

Ho trascorso metà della giornata leggendo tutte le domande e le risposte "Come cancellare una notifica locale". Dopo tutto, ho trovato la mia soluzione ma a quanto pare non funziona. Ho un Tableview con tutti i miei notifiche in programma ....Annulla notifica locale non funzionante

sul file H ho

@property (strong, nonatomic) UILocalNotification *theNotification; 

e poi sul file M: ​​

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    theNotification = [notificationArray objectAtIndex:indexPath.row]; 
    NSLog(@"Notification to cancel: %@", [theNotification description]); 
    // NSLOG Perfectly describes the notification to be cancelled. But then It will give me  "unrecognized selector" 


    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Local Reminder" 
                message:@"Cancel local reminder ?" 
                delegate:self 
              cancelButtonTitle:@"No" 
              otherButtonTitles:@"Yes", nil]; 
    [alertView show]; 
    [alertView release];  
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (buttonIndex == 0) { 
     NSLog(@"Cancel"); 
    }else{ 
     NSLog(@"Ok"); 
     [[UIApplication sharedApplication] cancelLocalNotification:theNotification]; 
    } 
} 

Se faccio clic su "Ok "Ottengo: 2012-02-04 03: 34: 48.806 Terzo test [8921: 207] - [__ NSCFType encodeWithCoder:]: selettore non riconosciuto inviato all'istanza 0x890ae90 Segnale ricevuto programma" SIGABRT ".

Se riesco a identificare completamente la notifica da cancellare, perché me lo dà?

risposta

12

Nella mia app ho fatto in questo modo:

- (IBAction)cancelLocalNotification:(id)sender 
{ 
    for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) 
    { 
     if ([[lNotification.userInfo valueForKey:@"FlightUniqueIDKey"] isEqualToString:flightNo]) 
     { 
      [[UIApplication sharedApplication] cancelLocalNotification:lNotification]; 
     } 
    } 
} 

E quando ho programmato la notifica locale, ho aggiunto un tasto. FlightNo è un ID univoco per la notifica.

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:flightNo forKey:@"FlightUniqueIDKey"]; 

localNotif.userInfo = infoDict; 

Nota da Nick Farina: questo funziona solo per le notifiche in programma; non riesci a cancellare una notifica presentata tramite presentLocalNotificationNow:

+2

Stavo cercando di trovare un modo in cui 1 - Non ho bisogno di chiudere il controller 2 - Visualizza la notifica che viene cancellata 3 - Non è necessario impostare una chiave specifica ogni volta. La tua risposta è totalmente valida, ho solo dovuto lavorare un po 'di più per un risultato visivo migliore. Grazie per la tua risposta però. Accetterò e voterò. – Farini

+2

Si noti che questo funziona solo per le notifiche _scheduled_; non riesci a cancellare una notifica presentata tramite 'presentLocalNotificationNow:'. Mi ci è voluto solo un anno per capirlo! –

+0

@Farini sentiti libero di modificare la mia risposta per renderla migliore :) – Shmidt

3

Ho trovato un modo per renderlo un po 'migliore. Se vuoi eliminare LocalNotification direttamente dalla tabella, puoi aggiungere un pulsante "cancella" o "cancella" su ogni cella. in questo modo:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row]; 
[cell.textLabel setText:notif.alertBody]; 

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"MM/ d/ YYYY"]; 
NSString *dateOnRecord = [dateFormat stringFromDate:notif.fireDate]; 

[cell.detailTextLabel setText:dateOnRecord]; 

UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
cancelButton.frame = CGRectMake(200, 5, 80, 34); 
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal]; 

cancelButton.titleLabel.textColor = [UIColor redColor]; 
cancelButton.backgroundColor = [UIColor colorWithRed:0.5 green:0.0 blue:0.0 alpha:1.0]; 
[cancelButton setTag:indexPath.row]; 
[cancelButton addTarget:self action:@selector(cancelNotification:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:cancelButton]; 
[dateFormat release]; 
return cell; 
} 

E poi si codifica il pulsante:

-(void)cancelNotification:(id)sender { 
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
UILocalNotification *notif = [notificationArray objectAtIndex:[sender tag]]; 
[[UIApplication sharedApplication] cancelLocalNotification:notif]; 
[self.tableView reloadData]; 
} 

Questo è solo un altro modo per farlo. Mi sembra un po 'meglio visualizzare.