8

在我的 iPad 应用程序中,我在一堂课上注册了一条通知:

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

我的selectedList:方法如下所示:

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

然后在另一个类(a UITableViewController)中,我在选择一行时发布该通知:

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

我可以确认正在发布通知,因为“发布通知”已记录到控制台,但从未调用“已收到通知”,这意味着未收到通知并且未调用选择器。我无法弄清楚是什么原因造成的。

谢谢

4

1 回答 1

16

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

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

于 2010-07-07T21:37:16.377 回答