访问 NSNotification 中传递的对象?
我有一个正在发布 NSDictionary 的 NSNotification:
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
anItemID, @"ItemID",
[NSString stringWithFormat:@"%i",q], @"Quantity",
[NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
[NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
nil];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:dict]];
如何订阅此内容并从此 NSDictionary 获取信息?
在我的 viewDidLoad 中,我有:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];
和类中的一个方法:
- (void)recieveInventoryUpdate:(NSNotification *)notification {
NSLog(@"%@ updated", [notification userInfo]);
}
当然,它会记录一个空值。
I have a NSNotification that is posting a NSDictionary:
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
anItemID, @"ItemID",
[NSString stringWithFormat:@"%i",q], @"Quantity",
[NSString stringWithFormat:@"%@",[NSDate date]], @"BackOrderDate",
[NSString stringWithFormat:@"%@", [NSDate date]],@"ModifiedOn",
nil];
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"InventoryUpdate" object:dict]];
How do I subscribe to this and get information from this NSDictionary?
in my viewDidLoad I have:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveInventoryUpdate:) name:@"InventoryUpdate" object:nil];
and a method in the class:
- (void)recieveInventoryUpdate:(NSNotification *)notification {
NSLog(@"%@ updated", [notification userInfo]);
}
which logs a null value of course.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你做错了。您需要使用:
并将字典传递给最后一个参数。您的“object”参数是发送通知的对象,而不是字典。
You are doing it wrong. You need to use:
and pass the dict to the last parameter. Your "object" parameter is the object sending the notification and not the dictionary.
斯威夫特:
Swift:
通知中的
对象
旨在作为发送者,在您的情况下,字典实际上并不是发送者,它只是信息。与通知一起发送的任何辅助信息都旨在与 userInfo 字典一起传递。像这样发送通知:然后像这样接收它,以良好的方式获得您想要的行为:
The
object
from the notification is intended to be the sender, in your case the dictionary is not actually the sender, its just information. Any auxiliary information to be sent along with the notification is intended to be passed along with theuserInfo
dictionary. Send the notification as such:And then receive it like this, to get the behavior you intend in a good way:
更简单的方法是
它对我有用。
More simpler way is
It worked for me.
简单答案
userInfo
传递数据(您的dict
)对象
object
是sender
=发布通知的对象
详细答案
完整代码应该是:
在您的
viewDidLoad中定义
用法 = 调用者
Simple Answer
userInfo
to pass data (yourdict
)object
object
is thesender
=The object which posting the notification
Detail Answer
full code should be:
definition
viewDidLoad
usage = caller
它是
[notification object]
,您还可以使用
notificationWithName:object:userInfo:
方法发送用户信息it's
[notification object]
you can also send userinfo by using
notificationWithName:object:userInfo:
method对象是发布通知的对象,而不是存储对象以便您可以访问它的方式。用户信息是您存储要与通知一起保留的信息的位置。
然后注册通知。该对象可以是您的类,也可以是 nil 以接收该名称的所有通知
接下来在您的选择器中使用它
Object is what object is posting the notification, not a way to store the object so you can get to it. The user info is where you store information you want to keep with the notification.
Then register for the notification. The object can be your class, or nil to just receive all notifications of this name
Next use it in your selector
很简单,看下面
如果访问
notification.userinfo
,它会返回null
。It's simple, see below
if access the
notification.userinfo
, it will returnnull
.