gpt4 book ai didi

objective-c - 未调用 NSNotificationCenter 选择器

转载 作者:太空狗 更新时间:2023-10-30 03:40:34 25 4
gpt4 key购买 nike

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

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

我的 selectedList: 方法如下所示:

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

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

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

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

谢谢

最佳答案

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

第二个最可能的原因是您在发布通知之前调用了 removeObserver:。这在 dealloc 中最常见(如果您曾经观察到任何东西,它应该总是调用 removeObserver)。这里的错误是您的观察对象在通知之前已解除分配。

关于objective-c - 未调用 NSNotificationCenter 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3198996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com