objective-c - 取消本地通知不起作用

标签 objective-c uitableview ios5 xcode4 uilocalnotification

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

在我的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”。

如果我可以完全确定要取消的通知,为什么它会给我那个?

最佳答案

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

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

当我安排本地通知时,我添加了一个 key 。 FlightNo 是通知的唯一 ID。

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

localNotif.userInfo = infoDict;

来自 Nick Farina 的注释:这仅适用于预定的通知;您似乎无法取消通过 presentLocalNotificationNow 呈现的通知:

关于objective-c - 取消本地通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139586/

相关文章:

ios - 如何以编程方式在 UITableView 上添加 UIView

ios - 如何停止 UITextView 滚动 UITableView

ios - JSON API 的新手如何访问 objective-c 中的值?

ios - 在 Objective C 的 FooterTableViewCell 中放置 View 的问题

c# - MonoTouch 对话框的 UITableView。如何处理水龙头?

ios - 如何以编程方式创建文件夹并将已安装的应用程序移动到此文件夹中

ios - 我的屏幕上正在模拟哪款 iPhone?

ios - 如何在 iOS 中从一个下拉菜单导航到下一个下拉菜单?

objective-c - 警告 : iPad: Icon-72. png:图标尺寸 (0 x 0)

ios - iPhone - 沿触摸和拖动路径移动对象