取消本地通知无效 [英] Cancel local notification not working
问题描述
我花了一半的时间阅读所有如何取消本地通知的问题和答案。
毕竟,我提出了自己的解决方案,但显然它不起作用。
我有一个包含我所有预定通知的tableview ....
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....
我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。
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 ?
推荐答案
在我的应用程序中,我这样做:
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];
}
}
}
当我安排本地通知时,我添加了一把钥匙。 FlightNo是通知的唯一ID。
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;
来自Nick Farina的注释:这仅适用于预定通知;您似乎无法取消通过 presentLocalNotificationNow
提供的通知:
Note from Nick Farina: this works for scheduled notifications only; you can't seem to cancel a notification presented via presentLocalNotificationNow
:
这篇关于取消本地通知无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!