objective-c 当 postNotificationName: 调用时未发送 NSNotification

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2113452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:49:19  来源:igfitidea点击:

NSNotification not being sent when postNotificationName: called

iphoneobjective-cnsnotificationcenter

提问by Affian

I'm trying to get one instance of using NSNotificationCenterwith addObserverand postNotificationNamebut I can't work out why it won't work.

我试图让使用的一个实例NSNotificationCenteraddObserverpostNotificationName,但我不能工作了,为什么它不会工作。

I have 2 lines to code to add the observer and send the message in 2 different classes

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

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

and

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

If I set the name to nilit works fine becuase it's just a broadcast, when i try and define a notification name the messages never get through.

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

采纳答案by James Raybould

All my code makes use of NSNotificationslike so:

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

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

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

The first one is registering the notification and the second posting of the notification.

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

回答by PostCodeism

Basically it's all to do with the order of execution. If you've executed postNotificationName before addObserver, then this is an easy problem to have. Use breakpoints and step through the code :)

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

Your first breakpoint should stop here:

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

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

Then here:

然后在这里:

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

Also, make sure the selector has a colon on. Because it's method signature will be:

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

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

回答by Jia Xiao

I had the same problem. The reason is that I called removeObserver method at

我有同样的问题。原因是我在调用 removeObserver 方法

- (void)viewDidDisappear:(BOOL)animated{

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

   [notificationCenter removeObserver:self];

}

So check whether if you had called removeObserver before postNotification.

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

Tips: You can search the keyword "removeObserver" to find if you had called this function.

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

回答by David Sowsy

Change this:

改变这个:

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

to this:

对此:

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

If your first notification is registered properly, newEventLoaded should be called.

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

回答by Brian Kalski

I had a similar issue and my problem was due to the notification being called on another thread. This solved my problem.

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

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

回答by JOM

Have you tried any other names but @"Event" and nil? Just to be sure, you could define your event names in one file and include that into both notification registration and sending. For example:

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

Header file:

头文件:

extern NSString * const NOTE_myEventName;

Source file:

源文件:

NSString * const NOTE_myEventName = @"MyEventName";

Registration:

登记:

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

Notification sending:

通知发送:

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

回答by heMac

I successfully fixed my "NSNotificationnot being sent when postNotificationName:called" crash.

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

I found the real bug is in notification message handler.

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

The postNotificationNameand addObserverare all right as the first post of this thread.

postNotificationNameaddObserver的所有权利,因为这线程的第一篇文章。