NSNotification addObserver object differentiation

In your init

what to do when the message is sent

Sending the message

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.

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:

Sending of those notifications as shown TNRadioButtonGroup.m:

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:

It simply means we will execute temperatureGroupUpdated: when we receive SELECTED_RADIO_BUTTON_CHANGED notifications, but only from the object self.temperatureGroup.