如何使用 NSNotificationCenter 传递对象
我试图将一个对象从我的应用程序委托传递到另一个类中的通知接收器。
我想传递整数 messageTotal
。现在我有:
在接收器中:
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"eRXReceived" object:nil];
在正在执行通知的类中:
[UIApplication sharedApplication].applicationIconBadgeNumber = messageTotal;
[[NSNotificationCenter defaultCenter] postNotificationName:@"eRXReceived" object:self];
但我想将对象 messageTotal
传递给另一个类。
I am trying to pass an object from my app delegate to a notification receiver in another class.
I want to pass integer messageTotal
. Right now I have:
In Receiver:
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissSheet) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"eRXReceived" object:nil];
In the class that is doing the notification:
[UIApplication sharedApplication].applicationIconBadgeNumber = messageTotal;
[[NSNotificationCenter defaultCenter] postNotificationName:@"eRXReceived" object:self];
But I want to pass the object messageTotal
to the other class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须使用“userInfo”变体并传递一个包含 messageTotal 整数的 NSDictionary 对象:
在接收端,您可以按如下方式访问 userInfo 字典:
You'll have to use the "userInfo" variant and pass a NSDictionary object that contains the messageTotal integer:
On the receiving end you can access the userInfo dictionary as follows:
在提供的解决方案的基础上,我认为展示一个传递您自己的自定义数据对象的示例可能会有所帮助(我在此处根据问题将其引用为“消息”)。
A 类(发送方):
B 类(接收方):
Building on the solution provided I thought it might be helpful to show an example passing your own custom data object (which I've referenced here as 'message' as per question).
Class A (sender):
Class B (receiver):
Swift 5
Bonus(你绝对应该这样做!):
将
Notification.Name("SomeNotificationName")
替换为.someNotificationName
:替换
"key0"
和"key1"
与Notification.Key.key0
和Notification.Key.key1
:为什么我一定要这样做?为了避免代价高昂的拼写错误,请享受重命名、享受查找用法等......
Swift 5
Bonus (that you should definitely do!) :
Replace
Notification.Name("SomeNotificationName")
with.someNotificationName
:Replace
"key0"
and"key1"
withNotification.Key.key0
andNotification.Key.key1
:Why should I definitely do this ? To avoid costly typo errors, enjoy renaming, enjoy find usage etc...
正如 @Johan Karlsson 指出的......我做错了。这是使用 NSNotificationCenter 发送和接收信息的正确方法。
首先,我们看一下 postNotificationName 的初始化程序:
source
我们将使用
userInfo
参数传递我们的信息。[NSObject : AnyObject]
类型是 Objective-C 的继承。因此,在 Swift 领域,我们需要做的就是传入一个 Swift 字典,其中包含从NSObject
派生的键和可以是AnyObject
的值。有了这些知识,我们创建一个字典,将其传递到
object
参数中:然后我们将字典传递到我们的 object 参数中。
发送者
接收者类
首先我们需要确保我们的类正在观察通知
然后我们可以接收我们的字典:
As @Johan Karlsson pointed out... I was doing it wrong. Here's the proper way to send and receive information with NSNotificationCenter.
First, we look at the initializer for postNotificationName:
source
We'll be passing our information using the
userInfo
param. The[NSObject : AnyObject]
type is a hold-over from Objective-C. So, in Swift land, all we need to do is pass in a Swift dictionary that has keys that are derived fromNSObject
and values which can beAnyObject
.With that knowledge we create a dictionary which we'll pass into the
object
parameter:Then we pass the dictionary into our object parameter.
Sender
Receiver Class
First we need to make sure our class is observing for the notification
Then we can receive our dictionary:
Swift 5.1 自定义对象/类型
Swift 5.1 Custom Object/Type