Search code examples
iosobjective-cnsnotificationcenter

Posting nsnotification but observer is not hearing it


I am using NSNotifiationCenter, like a 1000 other times, and post a notification, but my observers are not hearing it? What gives?

=== THIS IS IN MY 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];
}

=== THIS IS IN MY SOCKETMANAGER (socket manager is a singleton if that matters) ===

-(void)willStartService {

....

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

....

}

I have debugged and viewDidLoad is being called in my view. Yes, my willStartService is being called.


Solution

  • You are registering the TableView only for notifications sent by itself. It should be..

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