取消本地通知无效

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 Third test[8921:207] -[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x890ae90 程序接收到“SIGABRT”信号。
如果我能够完全确定要取消的通知,为什么还会出现这个问题?
2个回答

12

在我的应用中,我是这样做的:

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

当我安排本地通知时,我添加了一个键。FlightNo是通知的唯一标识符。

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

localNotif.userInfo = infoDict;

Nick Farina的注意事项: 这仅适用于计划通知; 似乎无法取消通过presentLocalNotificationNow提供的通知:


2
我试图找到一种方法,其中1-我不需要解除控制器2-可视化删除通知3-不必每次设置特定密钥。你的答案完全有效,只是我需要更努力地工作以获得更好的视觉效果。谢谢你的回答。我会接受并投票支持。 - Farini
2
请注意,这仅适用于“定时”通知;似乎无法取消通过presentLocalNotificationNow:呈现的通知。我花了一年时间才弄清楚! - Nick Farina
@Farini 如有需要可自由编辑我的答案,使其更加完善 :) - Shmidt

3

我找到了一种方法,可以让它看起来更好一些。如果你想直接从表格中删除本地通知,可以在每个单元格中添加一个“取消”或“删除”按钮,就像这样:

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

那只是另一种做法。在我看来,更好的方法是可视化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接