开发者

NSNotificationCenter selector not being called

开发者 https://www.devze.com 2023-01-05 21:51 出处:网络
In my iPad app, in one class I register for a notification: NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

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 cl开发者_C百科ass (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消