2012-02-04 2 views
7

"로컬 알림을 취소하는 방법"질문과 대답을 읽는 데 하루 중 절반을 보냈습니다. 결국, 내 솔루션을 생각해 냈지만 분명히 작동하지 않습니다. 내 모든 스케줄 통지와의 tableview이 ....가 H 파일에로컬 알림 취소 작동하지 않음

나는 M 파일에 다음

@property (strong, nonatomic) UILocalNotification *theNotification; 

하고 있습니다 : 내가 확인 "을 클릭하면

- (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 ".

취소 할 알림을 완전히 식별 할 수있는 이유는 무엇입니까? 내 응용 프로그램에서

답변

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; 

참고 : 이것은 단지 계획 알림 작동; presentLocalNotificationNow을 통해 표시된 알림을 취소 할 수 없습니다.

+2

컨트롤러를 닫을 필요가없는 방법을 찾으려고했습니다. 2 - 삭제되는 알림을 시각화합니다. 3 - 매번 특정 키를 설정할 필요가 없습니다. 귀하의 답변은 전적으로 유효합니다. 더 나은 시각적 결과를 위해 조금 더 노력해야했습니다. 그래도 고마워. 나는 받아 들여 투표 할 것이다. – Farini

+2

이것은 _scheduled_ 알림에만 적용됩니다. 'presentLocalNotificationNow : '를 통해 제시된 통지를 취소하는 것처럼 보이지 않을 수 있습니다. 그것은 단지 나에게 1 년이 걸리는 것을 알았다! –

+0

@Farini가 내 대답을 편집하여 자유롭게 편집 할 수 있습니다. :) – Shmidt

3

나는 조금 더 돋보일 수있는 방법을 발견했습니다. localNotification을 테이블에서 직접 삭제하려면 각 셀에 "취소"또는 "삭제"버튼을 추가하면됩니다. 과 같이 : 그것을 할 수있는 또 다른 방법

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

:

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

그리고 당신은 당신의 버튼을 코드입니다. 시각화하는 것이 나에게 조금 어울리는 것 같습니다.