Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access object passed in NSNotification?

Tags:

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.

like image 482
Slee Avatar asked Jul 19 '11 14:07

Slee


People also ask

How do you pass data in notifications?

Generally, we use a dictionary to send data using notification. We put information as key-value pair inside the dictionary and add that dictionary as a parameter while posting notification. In our case, we take a dictionary named “info” and put the text field inputs inside it as a key-value pair.

How do I use notification observer in Swift?

First, register an observer for a notification with: addObserver(_:selector:name:object:) Then, post a notification with post(name:object:userInfo:) … … after which your selector is called. And don't forget to remove the observer with removeObserver()

What is NS notification?

An object containing information broadcast to registered observers that bridges to Notification ; use NSNotification when you need reference semantics or other Foundation-specific behavior.


1 Answers

it's [notification object]

you can also send userinfo by using notificationWithName:object:userInfo: method

like image 142
alex-i Avatar answered Oct 05 '22 02:10

alex-i