【问题标题】:Cancel local notification not working取消本地通知不起作用
【发布时间】:2012-02-26 17:11:36
【问题描述】:

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

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

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

【问题讨论】:

    标签: objective-c uitableview ios5 xcode4 uilocalnotification


    【解决方案1】:

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

    - (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 提供的通知:

    【讨论】:

    • 我试图找到一种方法,其中 1 - 我不需要关闭控制器 2 - 可视化正在删除的通知 3 - 不必每次都设置特定的键。您的回答是完全有效的,我只需要多做一点工作以获得更好的视觉效果。不过谢谢你的回答。我会接受并投票。
    • 请注意,这仅适用于 scheduled 通知;您似乎无法取消通过presentLocalNotificationNow: 提供的通知。我只花了一年的时间就弄明白了!
    • @Farini 随时编辑我的答案以使其更好:)
    【解决方案2】:

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

    这只是另一种方式。在我看来,形象化要好一些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-29
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多