6

NSNotificationCenter에서 머리를 쓰려고합니다. 내 애플 리케이션 위임장에서 이와 비슷한 것을 가지고 있다면 :다른 등급의 NSNotification을 볼 수 있습니까?

어떻게 든 다른 뷰 컨트롤러에서 이것을 볼 수 있습니까? 필자의 경우, 테이블이있는 뷰 컨트롤러에서보고 그것을 알리고 알림을 받으면 테이블을 다시로드하고 싶습니다. 이것이 가능한가?

답변

4

네가 할 수있는 일은 NSNotification의 전체 목적입니다. 사용자가 원하는대로 View Controller를 추가해야만 응용 프로그램 위임자와 동일한 방식으로 알림을 받게됩니다.

여기에서 자세한 정보를 찾을 수 있습니다 : 그것은 가능

+1

그래서 원하는만큼 많은보기 컨트롤러에 추가 할 수 있습니까? – cannyboy

+1

맞습니다. – mopsled

+2

@Cannyboy 네, 한 통에 원하는만큼 옵서버를 추가 할 수 있습니다. –

2

물론 Notification Programming, 즉 통지의 요점입니다. addObserver:selector:name:object:을 사용하면 알림을 수신하기 위해 등록하는 방법 (테이블보기 컨트롤러에서이 작업을 수행해야 함)이며 postNotificationName:object:userInfo:을 사용하여 모든 클래스에서 알림을 게시 할 수 있습니다.

자세한 내용은 Notofication Programming Topics을 참조하십시오.

13

예는 다음과 같이 그것을 할 수 있습니다 :

을 클래스 A에서 : 클래스 B에서 알림

[[NSNotificationCenter defaultCenter] postNotficationName:@"DataUpdated "object:self]; 

를 게시 : 통지 먼저 등록하고 그것을 처리하는 방법을 쓰기. 메소드에 해당 선택자를 지정합니다.

//view did load 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUpdatedData:) name:@"DataUpdated" object:nil]; 


-(void)handleUpdatedData:(NSNotification *)notification { 
    NSLog(@"recieved"); 
} 
+1

그리고 관찰자를 어디에서 제거해야합니까? – Jatin

1

당신은 그냥 Observer로 그를 추가하고 당신이 그 통지가 게시 될 때 viewController 다르게 행동하는 것을 원하는 경우 다른 selector 방법을 제공해야합니다.

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(somethingOtherThing:) 
              name:@"something" 
               object:nil]; 


-(void)somethingOtherThing:(NSNotification *) notification 
{ 
// do something 

} 
2

원하는 등급으로 알림을 등록하려면 등록하십시오. 당신은 단순히 "헹구고 반복 할"필요가 있습니다. 당신의 방법에서있는 tableView를 다시로드 한 후 (아마도 : viewWillAppear에서 뷰 컨트롤러에서 관찰자로 등록 할 수있는 코드를 포함 :

- (void)viewWillAppear:(BOOL)animated { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(something:) name:@"something" object:nil]; 
} 

-(void)something:(NSNotification *) notification 
{ 
    [self.tableView reloadData]; 
} 

그것은 좋은 생각이기도 더 이상 한 번 뷰 컨트롤러를 해제를 등록하지 알림이 필요합니다.

- (void)viewWillDisappear:(BOOL)animated { 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 
관련 문제