NSNotificationCenter选择器未被调用 [英] NSNotificationCenter selector not being called

查看:97
本文介绍了NSNotificationCenter选择器未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPad应用程式中,在一个类别中,我注册一个通知:

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];

我的 selectedList:方法如下:

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

然后在另一个类( UITableViewController

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.

感谢

推荐答案

最可能的原因是你实际上并不调用 addObserver:selector:name:object:。你没有日志记录行;你确定代码正在运行吗?

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?

第二个最可能的原因是你在调用 removeObserver:则发布通知。这最常见于 dealloc (如果你曾经有过这种情况,应该总是调用 removeObserver 观察任何东西)。这里的错误是你的观察对象在通知之前已经释放。

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.

这篇关于NSNotificationCenter选择器未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆