In your init
1 2 3 4 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addImage:) name:INSERT_EVENT_IMAGE object:nil]; |
what to do when the message is sent
1 2 3 4 5 6 7 8 9 10 11 |
-(void)addImage:(NSNotification*)notification { if([[notification name] isEqualToString:INSERT_EVENT_IMAGE]) { NSDictionary *userInfo = notification.userInfo; UIImage * image = [userInfo objectForKey:@"image"]; // your code } } |
Sending the message
1 2 3 4 5 6 7 |
NSDictionary * packet = @{ @"image" : image }; [[NSNotificationCenter defaultCenter] postNotificationName:INSERT_EVENT_IMAGE object:self userInfo:packet]; |
When adding observers to receive notifications for a certain unique id, we can choose which object to receive it from. In our example below, we have method sexGroupUpdated: which will be executed once the notification is received for unique id SELECTED_RADIO_BUTTON_CHANGED. But we only will do this IFF those notifications are sent from self.sexGroup object.
1 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sexGroupUpdated:) name:SELECTED_RADIO_BUTTON_CHANGED object:self.sexGroup]; |
Extending our example, let’s say in our MainView.m, we have multiple objects of the same class TNRadioButtonGroup created. Those classes all send SELECTED_RADIO_BUTTON_CHANGED unique id notifications. We can differentiate which methods the notifications will hit by putting the name of the objects that are sending those notifications:
1 2 3 4 5 6 7 8 9 10 11 12 |
self.sexGroup = [[TNRadioButtonGroup alloc] initWithRadioButtonData:@[maleData, femaleData, alienData] layout:TNRadioButtonGroupLayoutHorizontal]; //blah blah [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sexGroupUpdated:) name:SELECTED_RADIO_BUTTON_CHANGED object:self.sexGroup]; self.hobbiesGroup = [[TNRadioButtonGroup alloc] initWithRadioButtonData:@[snowboardData,tennisData, programmingData] layout:TNRadioButtonGroupLayoutVertical]; ...blah blah.... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hobbiesGroupUpdated:) name:SELECTED_RADIO_BUTTON_CHANGED object:self.hobbiesGroup]; self.temperatureGroup = [[TNRadioButtonGroup alloc] initWithRadioButtonData:@[coldData, hotData, hotData1, hotData2] layout:TNRadioButtonGroupLayoutHorizontal]; ...blah blah.... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(temperatureGroupUpdated:) name:SELECTED_RADIO_BUTTON_CHANGED object:self.temperatureGroup]; |
Sending of those notifications as shown TNRadioButtonGroup.m:
1 2 3 4 5 6 7 8 9 |
- (void)setSelectedRadioButton:(TNRadioButton *)selectedRadioButton { if( _selectedRadioButton != selectedRadioButton ){ _selectedRadioButton = selectedRadioButton; [[NSNotificationCenter defaultCenter] postNotificationName:SELECTED_RADIO_BUTTON_CHANGED object:self]; } } |
It posts notification for constant string SELECTED_RADIO_BUTTON_CHANGED.
Let’s analyze in detail.
In our MainView.m, we’re adding an observer for notifications for string id SELECTED_RADIO_BUTTON_CHANGED.
But we’re getting it from three different instantiations of TNRadioButtonGroup:
sexGroup, hobbiesGroup, and temperatureGroup. They all send a notifications of id SELECTED_RADIO_BUTTON_CHANGED.
In other words, sexGroup, hobbiesGroup, and temperatureGroup all send those notifications. How do we differentiate them?
That’s where the paramter object comes in. As you noticed, we specified the object variable in which we receive these notifications:
1 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(temperatureGroupUpdated:) name:SELECTED_RADIO_BUTTON_CHANGED object:self.temperatureGroup]; |
It simply means we will execute temperatureGroupUpdated: when we receive SELECTED_RADIO_BUTTON_CHANGED notifications, but only from the object self.temperatureGroup.