Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNotificationCenter selector not being called

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

like image 755
indragie Avatar asked Jul 07 '10 21:07

indragie


1 Answers

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.

like image 171
Rob Napier Avatar answered Oct 13 '22 21:10

Rob Napier