2014-06-25 37 views
0

我使用NSNotifiationCenter,像其他1000次,併發布通知,但我的觀察員沒有聽到它?是什麼賦予了?發佈nsnotification,但觀察員沒有聽到它

===這是我泰伯維===

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(handleSuccessSocket) 
              name:kNotificationServiceStartSuccessful 
              object:self]; 


[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(handleFailedSocketConnection) 
              name:kNotificationSocketFailedToConnect 
              object:self]; 
} 

===這是我SOCKETMANAGER(插座管理器,如果該事項單身)===

-(void)willStartService { 

.... 

    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationServiceStartSuccessful object:nil]; 

.... 

} 

我調試了,viewDidLoad在我的視圖中被調用。是的,我的willStartService正在被調用。

回答

2

你只爲自己發送通知登記的TableView。它應該是..

[[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(handleSuccessSocket) 
             name:kNotificationServiceStartSuccessful 
             object:nil]; 
2

嘗試在您的-addObserver:selector:name:object方法調用中設置object:nil。你正在做的是告訴通知中心只有監聽來自表視圖實例的通知。

如果你不想通過nil你必須通過你的套接字管理器的一個實例。

檢查文檔: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object