当postNotificationName:调用时,不发送NSNotification [英] NSNotification not being sent when postNotificationName: called

查看:156
本文介绍了当postNotificationName:调用时,不发送NSNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 addObserver postNotificationName获取一个使用 NSNotificationCenter 的实例但我无法理解为什么它不起作用。

I'm trying to get one instance of using NSNotificationCenter with addObserver and postNotificationName but I can't work out why it won't work.

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

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

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

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

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

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

推荐答案

我所有的代码使用 NSNotifications ,如下所示:

All my code makes use of NSNotifications like so:

[[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.

这篇关于当postNotificationName:调用时,不发送NSNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆