Here is a simple example of posting and observing Notifications. It's a simple document-based application. There is an instance variable and a method declaration in the header:
And the code in the MyDocument.m implementation has just a little bit added to what the template gives. The top of thefile and the first part of init is:
There is a class variable N that is incremented each time init is called. The instantiated document saves the current value of N in its instance variable i. We declare and get a pointer to the default notification center, and log the fact that we're alive. And we declare a string (which should probably be like what Hillegass does:
extern NSString * const BNRColorChangedNotification;).
The interesting bit is next. It's a little fancy, we're using a dictionary called userInfo, which could be used to tell everybody everything. We could do without it.
The object (which is just nil above) could be used to inform the notification center that we are only interested in notifications that involve one particular object. Instead, here we get notifications for all the windows that are created.
Finally, we should do
Here is the output (log info removed and reformatted). When the app launches we get the log from the init method:
We create a second document, and following the log, the first document has received the notification of the second one's creation. It gets a pointer to the new document, which could be used for further communication.
Now we create a third document, and following the log, both the first and second documents have received the notification of the third one's creation: