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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With