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.
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.
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()
An object containing information broadcast to registered observers that bridges to Notification ; use NSNotification when you need reference semantics or other Foundation-specific behavior.
it's [notification object]
you can also send userinfo by using notificationWithName:object:userInfo:
method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With