2012-02-04 16 views
7

çalışan yerel bildirim iptal edin. Sonuçta, ben kendi çözüm geldi ama görünüşe çalışmıyor. Ben bütün planlanan bildirimleri ile bir tableView var .... H dosyadadeğil ben soruları ve cevapları "Bir yerel bildirim iptal Nasıl" Tüm okuma Günümün yarısını geçirdim

Ben M dosyada sonra

@property (strong, nonatomic) UILocalNotification *theNotification; 

ve var: Ben Tamam" tıklarsanız

- (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]; 
    } 
} 

"Ben olsun: 2012-02-04 03: 34: 48,806 Üçüncü testi [8921: 207] - [__ NSCFType encodeWithCoder:]: tanınmayan seçici 0x890ae90 Programı sinyali alınan örneğine gönderilen "SIGABRT".

Tamamen bildirim saptayabilirseniz

neden bana vermek gelmez iptal edilecek? Benim app

cevap

12

böyle yaptım:

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

Ve yerel bildirimi planlanan zaman, anahtarım ekledi. FlightNo, bildirim için benzersiz bir kimliktir. Nick Farina den

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

localNotif.userInfo = infoDict; 

Not: bu sadece planlanmış bildirimleri için çalışır; Eğer presentLocalNotificationNow aracılığıyla sunulan bir bildirim iptal etmek gibi olamaz:

+2

Ben 1 bir yol bulmaya çalışıyordu. Cevabınız tamamen geçerli, daha iyi bir görsel sonuç için biraz daha çalışmak zorunda kaldım. Cevabınız için teşekkürler. Kabul edip oy vereceğim. – Farini

+2

Bunun sadece _scheduled_ bildirimleri için çalıştığını unutmayın; presentLocalNotificationNow: ile sunulan bir bildirimi iptal edemezsiniz. Bu sadece anlamaya bir yıl sürdü! –

+0

@Farini daha iyi hale getirmek için cevabımı düzenlemekte çekinmeyin :) – Shmidt

3

Ben biraz daha iyi görünmesi için bir yol buldum. LocalNotification öğesini doğrudan tablodan silmek isterseniz, her bir hücreye bir "iptal" veya "sil" düğmesi ekleyebilirsiniz. şöyle: bunu yapmak için başka bir yolu

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

:

- (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; 
} 

Ve sonra düğmesi kodu. Bana görselleştirmek için biraz daha iyi görünüyor. Ben kontrolörü 2 görevden gerekmez - - kurulum için belirli bir anahtar her zaman var değil - bildirim 3 silinmesini Görselleştirin