发布通知但观察者没有听到它

时间:2014-06-25 16:36:49

标签: ios objective-c nsnotificationcenter

我正在使用NSNotifiationCenter,就像其他1000次一样,并发布通知,但我的观察员没有听到它?是什么给了什么?

===这是我的TABLEVIEW ===

- (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 个答案:

答案 0 :(得分:2)

尝试在object:nil方法调用中设置-addObserver:selector:name:object。您正在做的是告知通知中心侦听来自表格视图实例的通知。

如果您不想传递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

答案 1 :(得分:2)

您仅为自己发送的通知注册TableView。它应该是......

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(handleSuccessSocket)
                                         name:kNotificationServiceStartSuccessful
                                       object:nil];
相关问题