问题
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 ?
回答1:
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
:
回答2:
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.
来源:https://stackoverflow.com/questions/9139586/cancel-local-notification-not-working