我花了一半的时间阅读所有“如何取消本地通知”的问题和答案。 毕竟,我提出了自己的解决方案,但显然它无法正常工作。 我有一个包含所有预定通知的tableview ....
在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”。
如果我能完全确定要取消的通知,为什么会给我这个?
答案 0 :(得分:12)
在我的应用中,我这样做了:
- (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 :(得分:3)
我发现了一种让它看起来更好一点的方法。如果要直接从表中删除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];
}
这只是另一种方式。对我来说似乎有点可视化。