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