2012-02-04 79 views
7

我花了一天的時間閱讀所有「如何取消本地通知」問題和答案。畢竟,我想出了我自己的解決方案,但顯然它不工作。 我有我的所有計劃的通知的實現代碼如下....取消本地通知不起作用

在.h文件我已經

@property (strong, nonatomic) UILocalNotification *theNotification; 

,然後在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]; 
    } 
} 

如果我點擊「確定「我得到: 2012-02-04 03:34:48.806第三次測試[8921:207] - [__ NSCFType encodeWithCoder:]:無法識別的選擇器發送到實例0x890ae90 編程接收信號」SIGABRT「。

如果我完全可以識別要取消的通知爲什麼它會給我這個?

回答

12

在我的應用我做了這樣的:

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

當我安排本地通知,我添加了一個鍵。 FlightNo是通知的唯一ID。

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

localNotif.userInfo = infoDict; 

從尼克·法里納注:本工程只計劃通知;你似乎無法取消通過presentLocalNotificationNow提出的通知:

+2

我試圖找到一種方法,其中1 - 我不需要關閉控制器2 - 可視化通知被刪除3 - 不必每次都設置特定的按鍵。你的答案是完全有效的,我只是爲了獲得更好的視覺效果而多做一點工作。儘管感謝您的回答。我會接受並投票。 – Farini 2012-02-04 20:24:09

+2

請注意,這僅適用於_scheduled_通知;你似乎無法取消通過'presentLocalNotificationNow:'呈現的通知。這隻花了我一年的時間才搞清楚! – 2014-06-12 00:08:00

+0

@Farini隨時編輯我的答案,使其更好:) – Shmidt 2014-06-12 07:16:44

3

我找到了一種方法,可以讓它看起來更好一點。如果要從表中直接刪除localNotification,可以爲每個單元格添加一個「取消」或「刪除」按鈕。像這樣:

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

然後你編寫你的按鈕:

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

這只是另一種方式來做到這一點。看起來好像讓我想象一下。