如何使用postNotificationName:object传递NSDictionary: [英] How to pass a NSDictionary with postNotificationName:object:

查看:1244
本文介绍了如何使用postNotificationName:object传递NSDictionary:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSNotificationCenter将NSDictionary表单UIView传递给UIViewController。字典在发布通知时工作正常,但在接收方法中,我无法访问字典中的任何对象。

I am trying to pass an NSDictionary form a UIView to a UIViewController using NSNotificationCenter. The dictionary works fine at the time the notification is posted, but in the receiving method I am unable to access any of the objects in the dictionary.

这是我的样子创建字典并发布通知...

Here is how I am creating the dictionary and posting the notification...

itemDetails = [[NSDictionary alloc] initWithObjectsAndKeys:@"Topic 1", @"HelpTopic", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HotSpotTouched" object:itemDetails];

在UIViewController中我设置了观察者......

In the UIViewController I am setting the observer...

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

出于测试目的,hotSpotMore看起来像这样......

For testing purposes hotSpotMore looks like this...

- (void)hotSpotMore:(NSDictionary *)itemDetails{
      NSLog(@"%@", itemDetails);
      NSLog(@"%@", [itemDetails objectForKey:@"HelpTopic"]);    
}

第一个NSLog工作正常,显示字典的内容。第二个日志抛出以下异常......

The first NSLog works fine displaying the contents of the dictionary. The second log throws the following exception...

 [NSConcreteNotification objectForKey:]: unrecognized selector sent to instance 0x712b130

我不明白为什么我无法访问传递的字典中的任何对象。

I don't understand why I cannot access any objects in the passed dictionary.

提前感谢您的帮助。

John

推荐答案


第一个NSLog工作正常,显示字典内容

秒日志抛出以下
异常......

The first NSLog works fine displaying the contents of the dictionary. The second log throws the following exception...

程序试图欺骗你,它只是看起来它是你的字典,因为你的字典在通知中。从异常中可以看出,您的对象实际上来自名为NSConcreteNotification的类。

这是因为notification-method的参数始终是NSNotification-object。
这将有效:

The program tries to trick you, it just looks like it is your dictionary because your dictionary is inside the notification. From the exception you can see that your object actually is from a class named NSConcreteNotification.
This is because the argument of a notification-method is always a NSNotification-object. this will work:

- (void)hotSpotMore:(NSNotification *)notification {
      NSLog(@"%@", notification.object);
      NSLog(@"%@", [notification.object objectForKey:@"HelpTopic"]);    
}






正如提示:对象通常是发送通知的对象,你应该将你的NSDictionary作为userInfo发送。

我认为如果你这样做会改善你的代码:


just as a hint: the object is usually the object which sends the notification, you should send your NSDictionary as userInfo.
I think it would improve your code if you would do it like this:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:@"HotSpotTouched" object:self userInfo:itemDetails];


- (void)hotSpotMore:(NSNotification *)notification {
      NSLog(@"%@", notification.userInfo);
      NSLog(@"%@", [notification.userInfo objectForKey:@"HelpTopic"]);    
}

object参数用于区分可以发送通知的不同对象。$
假设您有两个不同的HotSpot对象都可以发送通知。在 addObserver:selector:name:object:中设置对象时,可以为每个对象添加不同的观察者对象。使用nil作为对象参数意味着应该接收所有通知,而不管发送通知的对象是什么。

The object parameter is used to distinguish between the different objects that can send a notification.
Let’s say you have two different HotSpot objects that can both send the notification. When you set the object in addObserver:selector:name:object: you can add a different observer for each of the objects. Using nil as the object parameter means that all notifications should be received, regardless of the object that did send the notification.

例如:

FancyHotSpot *hotSpotA;
FancyHotSpot *hotSpotB;

// notifications from hotSpotA should call hotSpotATouched:
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(hotSpotATouched:) name:@"HotSpotTouched" 
       object:hotSpotA]; // only notifications from hotSpotA will be received

// notifications from hotSpotB should call hotSpotBTouched:
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(hotSpotBTouched:) name:@"HotSpotTouched" 
       object:hotSpotB]; // only notifications from hotSpotB will be received

// notifications from all objects should call anyHotSpotTouched:
[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(anyHotSpotTouched:) name:@"HotSpotTouched" 
       object:nil]; // nil == "any object", so all notifications with the name "HotSpotTouched" will be received


- (void)hotSpotATouched:(NSNotification *)n {
    // only gets notification of hot spot A
}

- (void)hotSpotBTouched:(NSNotification *)n {
    // only gets notification of hot spot B
}

- (void)anyHotSpotTouched:(NSNotification *)n {
    // catches all notifications
}

这篇关于如何使用postNotificationName:object传递NSDictionary:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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