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
This is the best way to pass your dictionary data with NSNotification.
Post Notification :
Add Observer for Handle the Notification.
Put Notification Handler method.
I hope, this solution will help you
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:
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:
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
inaddObserver: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:
The method Matthias is talking about and the one I think you should be using is
Where object is the sender and userInfo is your dictionary.