取消本地通知不起作用

发布于 2025-01-02 09:13:52 字数 1642 浏览 0 评论 0原文

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

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

如果我可以完全识别要取消的通知,为什么它会给我这样的信息?

I've spent half of my day reading all "How to cancel a local notification" questions and answers.
After all, I came up with my own solution but apparently it is not working.
I have a tableview with all my scheduled notifications....

on the H file I have

@property (strong, nonatomic) UILocalNotification *theNotification;

and then on the M file:

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

If I click "Ok" I get:
2012-02-04 03:34:48.806 Third test[8921:207] -[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x890ae90
Program received signal "SIGABRT".

If I can totally identify the notification to be cancelled why does it give me that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉墨 2025-01-09 09:13:52

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

- (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 呈现的通知:

In my app I did it like this:

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

And when I scheduled local notification, I added a key. FlightNo is a unique ID for notification.

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

localNotif.userInfo = infoDict;

Note from Nick Farina: this works for scheduled notifications only; you can't seem to cancel a notification presented via presentLocalNotificationNow:

九歌凝 2025-01-09 09:13:52

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

这只是另一种方法。在我看来,形象化似乎更好一些。

I found a way that I can make it look a little better. If you want to delete the localNotification straight from the table, you can add a "cancel" or "delete" button to each cell. like so:

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

And then you code your button :

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

That's just another way to do it. Seems to me a little better to visualize.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文