jdog
jdog

Reputation: 10759

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.

Upvotes: 0

Views: 78

Answers (2)

Aaron
Aaron

Reputation: 7145

Try setting object:nil in your -addObserver:selector:name:object method calls. What you're doing is telling Notification Center to ONLY listen for notifications that come from the table view instance.

If you don't want to pass nil you'll have to pass an instance of your socket manager.

Check the Docs: 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:

Upvotes: 2

mvanallen
mvanallen

Reputation: 744

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];

Upvotes: 2

Related Questions