2012-02-04 40 views
7

Tôi đã dành nửa ngày để đọc tất cả các câu hỏi và câu trả lời "Cách hủy thông báo địa phương". Sau khi tất cả, tôi đã đưa ra giải pháp của riêng tôi nhưng dường như nó không hoạt động. Tôi có một tableview với tất cả các thông báo lịch trình của tôi ....Hủy thông báo địa phương không hoạt động

vào file H Tôi có

@property (strong, nonatomic) UILocalNotification *theNotification; 

và sau đó vào file 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]; 
    } 
} 

Nếu tôi bấm "Ok "Tôi nhận được: 2012-02-04 03: 34: 48.806 Kiểm tra thứ ba [8921: 207] - [__ NSCFType encodeWithCoder:]: bộ chọn không được nhận dạng được gửi tới trường hợp 0x890ae90 Tín hiệu nhận được chương trình" SIGABRT ".

Nếu tôi hoàn toàn có thể xác định được thông báo bị hủy, tại sao nó lại cho tôi điều đó?

Trả lời

12

Trong ứng dụng của tôi, tôi đã làm điều đó như thế này:

- (IBAction)cancelLocalNotification:(id)sender 
{ 
    for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) 
    { 
     if ([[lNotification.userInfo valueForKey:@"FlightUniqueIDKey"] isEqualToString:flightNo]) 
     { 
      [[UIApplication sharedApplication] cancelLocalNotification:lNotification]; 
     } 
    } 
} 

Và khi tôi lên kế hoạch thông báo địa phương, tôi đã thêm một chìa khóa. FlightNo là ID duy nhất để thông báo.

NSDictionary *infoDict = [NSDictionary dictionaryWithObject:flightNo forKey:@"FlightUniqueIDKey"]; 

localNotif.userInfo = infoDict; 

Lưu ý từ Nick Farina: chỉ hoạt động với thông báo được lên lịch; bạn dường như không thể hủy thông báo được trình bày qua presentLocalNotificationNow:

+2

Tôi đã cố gắng tìm cách mà 1 - Tôi không cần phải loại bỏ bộ điều khiển 2 - Hình dung thông báo bị xóa 3 - Không phải thiết lập khóa cụ thể mỗi lần. Câu trả lời của bạn là hoàn toàn hợp lệ, tôi chỉ phải làm việc nhiều hơn một chút để có kết quả trực quan tốt hơn. Cảm ơn câu trả lời của bạn mặc dù. Tôi sẽ chấp nhận và bỏ phiếu. – Farini

+2

Lưu ý rằng thao tác này chỉ hoạt động đối với các thông báo _scheduled_; bạn dường như không thể hủy thông báo được trình bày qua 'presentLocalNotificationNow:'. Điều đó chỉ mất một năm để tìm ra! –

+0

@Farini cảm thấy tự do để chỉnh sửa câu trả lời của tôi để làm cho nó tốt hơn :) – Shmidt

3

Tôi đã tìm ra cách để tôi có thể làm cho nó trông đẹp hơn một chút. Nếu bạn muốn xóa trực tiếpThông báo địa phương khỏi bảng, bạn có thể thêm nút "hủy" hoặc "xóa" vào từng ô. như vậy:

- (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; 
} 

Và sau đó bạn mã nút của bạn:

-(void)cancelNotification:(id)sender { 
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
UILocalNotification *notif = [notificationArray objectAtIndex:[sender tag]]; 
[[UIApplication sharedApplication] cancelLocalNotification:notif]; 
[self.tableView reloadData]; 
} 

Đó chỉ là một cách khác để làm điều đó. Dường như với tôi tốt hơn một chút để hình dung.

Các vấn đề liên quan