【问题标题】:NSNotification not being sent when postNotificationName: calledpostNotificationName: 调用时未发送 NSNotification
【发布时间】:2010-01-21 22:29:54
【问题描述】:

我正在尝试使用 NSNotificationCenteraddObserverpostNotificationName 的一个实例,但我不知道为什么它不起作用。

我有 2 行代码来添加观察者并在 2 个不同的类中发送消息

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded:) name:@"Event" object:nil];

[[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];

如果我将名称设置为nil,它可以正常工作,因为它只是一个广播,当我尝试定义一个通知名称时,消息永远不会通过。

【问题讨论】:

    标签: iphone objective-c nsnotificationcenter


    【解决方案1】:

    我所有的代码都像这样使用NSNotifications

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"ScanCompleted" object:nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ScanCompleted" object:nil];
    

    第一个是注册通知,第二个是发布通知。

    【讨论】:

    • 这正是我所拥有的,但它拒绝工作,让我认为问题出在其他地方,但通知中心似乎在代码方面是完全独立的。我不知道在哪里可以找到可能导致问题的原因。可能是线程? iPhone会自动多线程吗?我不知道。
    • 选择器中的 NSLog 语句(在我的情况下为 updateView)是否正常工作?如果您的方法不带任何参数,请尝试不带 : so [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(newEventLoaded) name:@"Event" object:nil]; 的方法名称
    • 我所有的测试都是在所选方法中使用 NSLog 完成的,当我将通知程序的名称设置为 nil 时它就可以工作。我也试过方法中带参数和不带参数的
    • 呸,你最终做了什么样的重构?我也有同样的问题,而且很麻木。我什至在注册后直接添加了 postNotification,使用日志记录语句和断点来验证这些行正在执行.. 什么都没有
    • 我知道这是一个相当古老的答案,但以防万一它可以帮助某人比我更快地解决同样的问题。在我的情况下,我愚蠢地没有强烈引用正在观察通知的控制器,所以它在它可以收听它之前就被释放了。
    【解决方案2】:

    基本上这与执行顺序有关。如果您在 addObserver 之前执行了 postNotificationName,那么这是一个容易遇到的问题。使用断点并单步执行代码:)

    你的第一个断点应该停在这里:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"ScanCompleted" object:nil];
    

    那么这里:

    [[NSNotificationCenter defaultCenter]postNotificationName:@"ScanCompleted" object:self];
    

    另外,请确保选择器上有一个冒号。因为它的方法签名将是:

    - (void)updateView:(NSNotification *)notification;
    

    【讨论】:

    • 感谢您提醒我 postNotificationNameaddObserver 之前被调用。它解决了我的问题。
    【解决方案3】:

    我遇到了同样的问题。 原因是我在

    调用了 removeObserver 方法
    - (void)viewDidDisappear:(BOOL)animated{
    
        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    
       [notificationCenter removeObserver:self];
    
    }
    

    因此请检查您是否在 postNotification 之前调用了 removeObserver。

    提示:您可以搜索关键字“removeObserver”来查找您是否调用过此函数。

    【讨论】:

    • 请使用答案下方的edit 链接更正代码格式。
    • 同样的事情发生在我身上。错过了一些在我添加观察者后实际上删除所有观察者的代码。毫不奇怪,它不起作用。
    【解决方案4】:

    改变这个:

    [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
    

    对此:

    [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:nil];
    

    如果您的第一个通知已正确注册,则应调用 newEventLoaded。

    【讨论】:

    • 不走运,我已经尝试了对象值和事件值的各种组合,但我唯一可以工作的似乎是将名称设置为 nil,除非我遗漏了什么在文档中。
    【解决方案5】:

    我遇到了类似的问题,我的问题是由于在另一个线程上调用了通知。这解决了我的问题。

    dispatch_async(dispatch_get_main_queue(),^{
        [[NSNotificationCenter defaultCenter]postNotificationName:@"Event" object:self];
    });
    

    【讨论】:

      【解决方案6】:

      除了@"Event" 和 nil 之外,您是否尝试过其他名称?可以肯定的是,您可以在一个文件中定义您的事件名称,并将其包含在通知注册和发送中。例如:

      头文件:

      extern NSString * const NOTE_myEventName;
      

      源文件:

      NSString * const NOTE_myEventName = @"MyEventName";
      

      注册:

      [[NSNotificationCenter defaultCenter]
       addObserver:self
          selector:@selector(handleMyEvent:)
              name:NOTE_myEventName
            object:nil];
      

      通知发送:

      [[NSNotificationCenter defaultCenter]
          postNotificationName:NOTE_myEventName object:nil];
      

      【讨论】:

        【解决方案7】:

        我成功修复了“postNotificationName: 调用时未发送NSNotification”崩溃问题。

        我发现真正的错误在于通知消息处理程序。

        postNotificationNameaddObserver 可以作为这个帖子的第一个帖子。

        【讨论】:

        • 消息处理程序有什么问题?你是怎么解决的?
        猜你喜欢
        • 2016-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-16
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        相关资源
        最近更新 更多