2014-10-15 3 views
0

내가 푸시 알림에 대한 책임있는 방법으로, AppDelegate에AppDelegate.m에서 TableView 데이터를 다시로드 하시겠습니까?

AppDelegate.m에서

에서있는 tableView를 다시로드하려고, "didReceiveRemoteNotification는"나는 UIAlertView 매번 전화 통지가 도착했을 때.

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Notification received" message:[NSString stringWithFormat:@"%@", titleMsg] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
       alertView.delegate = self; 
       [alertView show]; 

사용자가 "OK"버튼을 클릭하면, 데이터베이스의 읽기하고 난 UIAlertViewDelegate

프로토콜에 추가 된 헤더에있는 tableview

// Reload the table when the user click "OK" button in the alert 
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
if ([alertView.title isEqualToString:@"Notification received"]) 
{ 
    if (buttonIndex == [alertView cancelButtonIndex]) 
    { 
     // Stop the sound for notifications 
     [self stopSoundForNotifications]; 

     // Refresh table messages 
     AccueilViewController * avc = [[AccueilViewController alloc] init]; 
     [avc readMsgsFromDB]; 
     [avc reloadTableMsgsReceived]; 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"ReadMessagesFromDB" object:nil]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadDataFromDelegate" object:nil]; 
    } 
} 

}

를 다시로드한다

몇 가지 아이디어가 있으십니까? (UIAlertView *) alertView clickedButtonAtIndex : (NSInteger) buttonIndex 것 같습니다

if ([alertView.title isEqualToString:@"Notification received"]) 
{ 
    if (buttonIndex == [alertView OKButtonIndex]) 
    { 
     // then fire notification 
     // Stop the sound for notifications 
     [self stopSoundForNotifications]; 

     // Refresh table messages 
     AccueilViewController * avc = [[AccueilViewController alloc] init]; 
     [avc readMsgsFromDB]; 
     [avc reloadTableMsgsReceived]; 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"ReadMessagesFromDB" object:nil]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadDataFromDelegate" object:nil]; 
    } 
} 
+0

무엇이 문제입니까? 알림이 호출되지 않고 있습니까? – Bejibun

답변

0

당신은 OK 버튼을 에 조건을 넣어 대신 그 취소 버튼에 조건을 넣어 기존 인스턴스에 액세스하는 대신 AccueilViewController의 새 인스턴스를 만드는 것과 같습니다. 따라서 readMsgsFromDBreloadTableMsgsReceived은 보이지 않는 인스턴스에 의해 처리되고 있습니다.

AccueilViewController 코드를 수정하면 해당 알림을 수신하도록 등록 할 수 있으며 기존 인스턴스는 코드를 사용하여 데이터를 다시로드 할 수 있습니다. 사실, 하나의 알림 만 사용하고 두 메서드 호출을 하나로 결합합니다.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived) name:@"ReadMessagesFromDB" sender:nil]; 

는 그런 다음 두 가지 작업을 결합하는 방법을 추가 :

그래서 당신의 AccueilViewController 당신의 viewDidLoad 방법에 다음을 추가 두 번째 postNotification을 삭제에서

-(void)notificationReceived { 
    [self readMsgsFromDB]; 
    [self reloadTableMsgsReceived]; 
} 

그래서 때를 AlertView 버튼을 누르면 알림이 게시됩니다. 기존 AccueilViewController 인스턴스의 notificationReceived 메서드가 실행되어 메시지가 데이터베이스에서 읽히고 테이블이 다시로드됩니다. 알림 센터에서보기 컨트롤러를 제거하기 전에 호출을 추가하여 할당 해제해야합니다.

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ReadMessagesFromDB" object:nil]; 
0

을 {(무효) alertView - 감사

관련 문제