출처 : http://iphonedevelopertips.com/cocoa/basics-of-notifications.html
What follows is a brief guide to working with Notifications in Cocoa. I’ll cover the basics, including registering an observer and posting notifications, just enough to start using notifications in your iPhone apps.
There is an instance of NSNotificationCenter available to every running iPhone application. This class acts as an intermediary to facilitate communication between objects that are interested in being notified at some point in the future (these objects are known as the observers) and a poster that posts to the notification center, resulting in all observers (registered for a specific notification) being called.
To give you an idea of where you might use notifications, consider how you might handle downloading of data in a background thread. I recently used notifications in this scenario as I wanted to be notified when a web-service call completed. Upon receiving a notification, I then proceeded to populate a view with the data retrieved, or with an error message if the data access failed.
The 30,000 foot view consists of two steps:
Step #1
Register an observer, which requests that a selector to be called when a specific notification is posted.
Step #2
Post a notification, which will result in all registered observers being called.
For example:
This code registers an observer for a notification with the name NOTIF_DataComplete (more on that below). When a notification is posted with the same name, the methoddownloadDataComplete will be called.
Posting a notification with the name NOTIF_DataComplete would look as follows:
Let’s show how this might like in a working example.
Class Registering as Observer
Below is the code for the implementation of SomeClass. Notice on line 6 the name of the notification is defined. On line 40 we register as an observer for the named notification, specifying the selector @selector(downloadDataComplete:). Once the notification is posted, the method on line 26 is called.
Class That Posts Notification
To keep things simple, I’ll add a few lines to the applicationDidFinishLaunching method to show you how posting a notification might look:
I create an instance of SomeClass and follow this by posting of a notification of the nameNOTIF_DataComplete. This example is a little contrived, however, I’m sure you get the idea. Although a short example, it shows just how easy it is to register an observer and post notifications.
There are various other nuances to working with notifications that can be quite helpful when the time comes that you need more robust support for communication among objects. For example, you could specify that you are interested in receiving a notification by name (as above), however, only if that notification has a specific object affiliated with it. This would allow you to use the same notification name, however, only notify certain objects. One use of this could be if you have multiple downloads and want to use notifications to be notify a specific object that it’s download is complete.
If you are working with multiple threads, which would be the case if you need to fire off multiple downloads in the background, you can look into distributed notifications, which allow notifications to be delivered to a particular thread. For more information, you can readIntroduction to Notification Programming Topics available in the iPhone Reference Library.