2009-06-03 3 views
2

내가 작업중인 iPhone 응용 프로그램에서 사용자 정의 클래스를 사용하여 호스트와 네트워크 통신을 관리합니다. protocolClass라는 클래스는 appDelegate의 ivar이고 applicationDidFinishLaunching : 메소드의 alloc + init입니다.appDelegate에서 정보를 UINavigationcontroller의보기 컨트롤러 중 하나로 전달하는 방법

이제 protocolClass가 호스트로부터 데이터를받을 때마다 delegate (appDelegate로 설정)에서 protocolClassDidReceiveData : 메소드를 호출합니다. 그런 다음 UINavigatorController의 customViewController 중 하나에서 데이터를 업데이트해야합니다.

appDelegate에서 업데이트해야하는 customViewController에 대한 참조를 추가해야합니까? 아니면 좀 더 효율적인 방법이 있습니까?

customViewcontroller에 대한 참조를 유지하려면 메모리 사용량이 무엇입니까?

미리 감사드립니다.

답변

2

맞다면 관련없는 프로그램에서 이벤트가 발생한 후에보기를 업데이트하고 싶습니다.

코드에서 종속성 수를 줄이려면 더 밀접하게 결합 된 인스턴스 변수 대신 NSNotification을 사용하는 것이 좋습니다. 알림은 코코아 컨셉으로, 코드의 일부가 모든 청취자가 등록 할 수있는 이벤트와 유사한 메시지를 내 보냅니다. 귀하의 경우에는

그 결과는 다음과 같습니다

AppDelegate에 헤더 :

extern NSString* kDataReceived; 

AppDelegate에 구현 : 일부 관심 리스너 클래스의 구현에

NSString* kDataReceived = @"DataReceived"; 

- (void)protocolClassDidReceiveData:(NSData*)data { 
    [[NSNotificationCenter defaultCenter] postNotificationName:kDataReceived 
                 object:self 
                 userInfo:data]; 
} 

(예를 들어, 당신의 UIViewController) :

// register for the notification somewhere 
- (id)init 
{ 
    self = [super init]; 
    if (self != nil) { 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(dataReceivedNotification:) 
                name:kDataReceived 
                object:nil]; 
    } 
} 

// unregister 
- (void)dealloc 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

// receive the notification 
- (void)dataReceivedNotification:(NSNotification*)notification 
{ 
    NSData* data = [notification userInfo]; 
    // do something with data 
} 
+0

감사합니다. Nikolai, 감사 센터를 확인해 드리겠습니다. 처음에는 불필요한 시스템 리소스를 사용한다는 의미의 notificationCenter 사용에 대해 걱정했습니다. – Ben

+0

나는 조기 최적화라고 생각한다. 모든보기에서 얼마나 많은 알림이 돌아가고 있는지 살펴 본다면 소켓에서 데이터를받은 후 알림을 게시하는 데 아무런 해가되지 않을 것이라고 생각합니다. –

+0

감사합니다. Nikolai! 나는 아이폰에 전달 이벤트를 인터넷 검색하고 있었고 귀하의 게시물을 보았다. – ambertch

2

예, 알림을 사용하는 것이 좋습니다. 그리고 모델이 컨트롤러를 업데이트하려고 할 때 (즉, ViewController] - 통지는 좋은 방법입니다. 제 경우에는 SSDP (AsyncUdpSocket 사용)를 사용하여 장치를 검색하려고하는데 장치를 찾으면 내 View Controller를 업데이트/알리고 싶었습니다. 이것은 비동기식이기 때문에 데이터가 수신되면 알림을 사용했습니다. 여기에 간단한 내가 한 일이 다음의 viewDidLoad에서

(나는 초기화를 무시하려하지만 나를 위해 잘 작동하지 않았다) - 다음과 같이 내가 통지 내의 ViewController 등록 :

*NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 
    [nc addObserver:self 
      selector:@selector(foundController:) 
       name:@"DiscoveredController" 
      object:nil]; 

다음은 내의 ViewController의 선택 : 내 "모델"에서

// receive the notification 
- (void)foundController:(NSNotification *)note 
{ 
    self.controllerFoundStatus.text = @"We found a controller"; 
} 

[앱이 아닌 대리인이 - 다음과 같이 내가이 장치를 검색하는 데 사용하는 새로운 클래스를 만들었습니다 "serviceSSDP"내가 한 모든 알림을 게시했다 :

[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoveredController" object:nil]; 

그게 전부입니다.이 알림은 SSDP 검색에 대한 올바른 응답을받을 때 표시됩니다. [구체적으로는 :

- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock 
    didReceiveData:(NSData *)data 
      withTag:(long)tag 
      fromHost:(NSString *)host 
       port:(UInt16)port 

의 AsyncUdpSocket입니다.

관련 문제