Objective c 取消本地通知不起作用

Objective c 取消本地通知不起作用,objective-c,uitableview,ios5,xcode4,uilocalnotification,Objective C,Uitableview,Ios5,Xcode4,Uilocalnotification,我花了半天时间阅读了所有“如何取消本地通知”的问题和答案。 毕竟,我提出了自己的解决方案,但显然它不起作用。 我有一个包含所有预定通知的tableview 在我的H档案上 @property (strong, nonatomic) UILocalNotification *theNotification; 然后在M文件上: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)inde

我花了半天时间阅读了所有“如何取消本地通知”的问题和答案。 毕竟,我提出了自己的解决方案,但显然它不起作用。 我有一个包含所有预定通知的tableview

在我的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]-[\uu NSCFType encodeWithCoder:]:发送到实例0x890ae90的选择器无法识别 程序接收到信号“SIGABRT”


如果我能完全确定要取消的通知,为什么它会给我这样的信息?

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

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

Nick Farina的说明:这仅适用于计划通知;您似乎无法取消通过
presentLocalNotificationNow

显示的通知。我找到了一种方法,可以让它看起来更好一点。如果要直接从表中删除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];
}

这只是另一种方法。在我看来,可视化似乎更好。

我试图找到一种方法,1-我不需要关闭控制器2-可视化正在删除的通知3-不必每次都设置特定的键。你的答案是完全正确的,我只是为了更好的视觉效果需要多做一些工作。谢谢你的回答。我会接受并投赞成票。请注意,这只适用于计划通知;您似乎无法取消通过
presentLocalNotificationNow:
显示的通知。我只花了一年的时间就明白了@Farini可以随意编辑我的答案,使之更好:)