如何使用 postNotificationName:object 传递 NSDictionary:

发布于 2024-10-01 09:13:06 字数 1116 浏览 0 评论 0原文

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

这是我创建字典并发布通知的方式...

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

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

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

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

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

第一个 NSLog 可以很好地显示字典的内容。第二个日志抛出以下异常...

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

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

预先感谢您的任何帮助。

约翰

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];

In the UIViewController I am setting the observer...

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

For testing purposes hotSpotMore looks like this...

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

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.

Thanks in advance for any help.

John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天荒地未老 2024-10-08 09:13:06

第一个 NSLog 可以正常显示
字典的内容。这
第二条日志抛出以下内容
异常...

该程序试图欺骗您,它只是看起来像是您的字典,因为您的字典位于通知内。从异常中您可以看到您的对象实际上来自一个名为 NSConcreteNotification 的类。
这是因为通知方法的参数始终是 NSNotification 对象。
这将起作用:

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

就像提示:该对象通常是发送通知的对象,您应该将 NSDictionary 作为 userInfo 发送。
我认为如果您这样做,它会改进您的代码:

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


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

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

例如:

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
}

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

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"]);    
}

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"]);    
}

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.

E.g:

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
}
伴我心暖 2024-10-08 09:13:06

这是使用 NSNotification 传递字典数据的最佳方式。

发布通知:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"Put Your Notification Name" object:self userInfo:"Pass your dictionary name"];

添加观察者来处理通知。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mydictionaryData:)  name:@"Put Your Notification Name" object:nil];

Put 通知处理程序方法。

- (void)mydictionaryData::(NSNotification*)notification{
   NSDictionary* userInfo = notification.userInfo;
   NSLog (@"Successfully received test notification! %@", userInfo);}

我希望这个解决方案能帮助您

This is the best way to pass your dictionary data with NSNotification.

Post Notification :

 [[NSNotificationCenter defaultCenter] postNotificationName:@"Put Your Notification Name" object:self userInfo:"Pass your dictionary name"];

Add Observer for Handle the Notification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mydictionaryData:)  name:@"Put Your Notification Name" object:nil];

Put Notification Handler method.

- (void)mydictionaryData::(NSNotification*)notification{
   NSDictionary* userInfo = notification.userInfo;
   NSLog (@"Successfully received test notification! %@", userInfo);}

I hope, this solution will help you

冬天旳寂寞 2024-10-08 09:13:06

Matthias 正在谈论的方法,我认为你应该使用的方法是

postNotificationName:object:userInfo:

Where object 是发送者,userInfo 是你的字典。

The method Matthias is talking about and the one I think you should be using is

postNotificationName:object:userInfo:

Where object is the sender and userInfo is your dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文