在我的 iPad 应用程序中,我在一堂课中注册了一条通知:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(selectedList:) name:@"TTSelectedList" object:nil];
My selectedList:
方法如下所示:
- (void)selectedList:(NSNotification*)notification
{
NSLog(@"received notification");
}
然后在另一个班级(aUITableViewController
)当选择一行时我发布该通知:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"posting notification");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TTSelectedList" object:nil];
}
我可以确认通知正在发布,因为“发布通知”已记录到控制台,但“收到的通知”从未被调用,这意味着未收到通知并且尚未调用选择器。我不明白是什么原因造成的。
Thanks
最可能的原因是您实际上并没有打电话addObserver:selector:name:object:
。那里没有伐木线;你确定代码正在运行吗?
第二个最可能的原因是您正在打电话removeObserver:
在发布通知之前。这最常见于dealloc
(这应该always call removeObserver
如果你曾经观察过任何事情)。这里的错误是您的观察对象在通知之前已释放。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)