views:

233

answers:

3

I'm trying to get one instance of using NSNotificationCenter with addObserver and postNotificationName but I can't work out why it won't work.

I have 2 lines to code to add the observer and send the message in 2 different classes

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded:) name:@"Event" object:nil];

and

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];

If I set the name to nil it works fine becuase it's just a broadcast, when i try and define a notification name the messages never get through.

+1  A: 

Change this:

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];

to this:

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

If your first notification is registered properly, newEventLoaded should be called.

David Sowsy
No luck, I've tried various combinations of the object values and the event values but the only one that I can get working it seems is having the name set as nil, unless I'm missing something in the documentation.
Affian
A: 

All my code makes use of NSNotifications like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"ScanCompleted" object:nil];

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

The first one is registering the notification and the second posting of the notification.

James Raybould
This is exactly what I have yet it refuses to work, leads me to think that the issue is elsewhere but the Notification center seems to be quite self contained code-wise. I haven't a clue where else to look for what could be causing the issue. Threading maybe? Does the iPhone automaticly multi-thread at all? Not that I know of.
Affian
Does an NSLog statement in the selector (in my case updateView) work properly? If your method doesn't take any parameters try the method name without the : so `[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded) name:@"Event" object:nil];`
James Raybould
All my testing has been done with a NSLog in the in the selected method and it works when I set the notifier's name to nil. I've also tried it both with and without parameters in the method
Affian
I still don't know why it wasn't working but after a bit of refactoring it's working fine now with the same 2 lines of code as per the documentation.
Affian
A: 

Have you tried any other names but @"Event" and nil? Just to be sure, you could define your event names in one file and include that into both notification registration and sending. For example:

Header file:

extern NSString * const NOTE_myEventName;

Source file:

NSString * const NOTE_myEventName = @"MyEventName";

Registration:

[[NSNotificationCenter defaultCenter]
 addObserver:self
    selector:@selector(handleMyEvent:)
        name:NOTE_myEventName
      object:nil];

Notification sending:

[[NSNotificationCenter defaultCenter]
    postNotificationName:NOTE_myEventName object:nil];
JOM