8

In my iPad app, in one class I register for a notification:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(selectedList:) name:@"TTSelectedList" object:nil];

My selectedList: method looks like this:

- (void)selectedList:(NSNotification*)notification
{
    NSLog(@"received notification");
}

Then in another class (a UITableViewController) I post that notification when a row is selected:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"posting notification");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TTSelectedList" object:nil];
}

I can confirm that the notification is being posted, because "posting notification" is logged to the console, but "received notification" is never called, meaning that the notification isn't received and the selector hasn't been called. I can't figure out what's causing this.

Thanks

indragie
  • 18,002
  • 16
  • 95
  • 164
  • 1
    Something stupid, but which left me puzzled for a while. I had the same issue. In my case, I didn't notice a subclass had method with the same selector as the one I was trying to declare in the parent class. – Michael Mior Sep 04 '12 at 23:07
  • Had this myself, also something stupid: I was posting a notification with a lowercase letter instead of a capital letter. They're case sensitive! – Ruben Martinez Jr. Jan 06 '15 at 08:40
  • In my case I created an object in local method, So the observer class dealloc during notification fire from another class. You can check this: https://stackoverflow.com/questions/67328439/nsnotificationcenter-addobserver-not-working-in-release-mode-but-works-in-debug – Ravi Apr 30 '21 at 06:24

1 Answers1

16

The most likely cause is that you're not actually calling addObserver:selector:name:object:. You don't have a logging line there; are you sure that code is running?

The second most likely cause is that you're calling removeObserver: before the notification is posted. This is most commonly in dealloc (which should always call removeObserver if you've ever observed anything). The error here would be that your observing object has deallocated before the notification.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • You were right, I was creating the object that was the observer in Interface Builder, and it wasn't being retained by anything. Thanks. – indragie Jul 07 '10 at 21:44
  • I had the same problem. I added the observer in the `init` method instead of `initWithStyle` – testing Nov 01 '10 at 19:13
  • 1
    If you use IB to make your stuff, chances are that init will not get called. Use initWithCoder or awakeWithNib depending on what you want to do. – futureelite7 Aug 06 '11 at 12:56
  • In my case issue with the local instance of listener class. Please check and vote if helps: https://stackoverflow.com/questions/67328439/nsnotificationcenter-addobserver-not-working-in-release-mode-but-works-in-debug – Ravi Apr 30 '21 at 05:54