未调用NSNOTIFICYCENTER选择器
-
02-10-2019 - |
题
在我的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];
}
我可以确认已发布该通知,因为“发布通知”已记录到控制台,但是“收到的通知”从未被调用,这意味着未收到通知,并且未接收选择器。我不知道是什么原因引起的。
谢谢
解决方案
最有可能的原因是您实际上没有打电话 addObserver:selector:name:object:
. 。您在那里没有伐木线;您确定代码正在运行吗?
第二个最有可能的原因是您正在打电话 removeObserver:
在发布通知之前。这是最常见的 dealloc
(应该 总是 称呼 removeObserver
如果您曾经观察到任何东西)。这里的错误是,您的观察对象在通知之前已交易。
不隶属于 StackOverflow