取消本地通知不起作用
我花了半天时间阅读所有“如何取消本地通知”问题和答案。 毕竟,我想出了自己的解决方案,但显然它不起作用。 我有一个包含所有预定通知的表格视图......
在我拥有的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我的应用程序中,我是这样做的:
当我安排本地通知时,我添加了一个键。 FlightNo 是用于通知的唯一 ID。
尼克·法里纳 (Nick Farina) 的注释:这仅适用于预定通知;您似乎无法取消通过
presentLocalNotificationNow
呈现的通知:In my app I did it like this:
And when I scheduled local notification, I added a key. FlightNo is a unique ID for notification.
Note from Nick Farina: this works for scheduled notifications only; you can't seem to cancel a notification presented via
presentLocalNotificationNow
:我找到了一种可以让它看起来更好一点的方法。如果您想直接从表格中删除 localNotification,您可以向每个单元格添加“取消”或“删除”按钮。像这样:
然后你对按钮进行编码:
这只是另一种方法。在我看来,形象化似乎更好一些。
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:
And then you code your button :
That's just another way to do it. Seems to me a little better to visualize.