Chapter 5. Notifications and implementing the observer pattern

 

This chapter covers

  • Notifying multiple objects
  • Posting and queuing notifications
  • Observing notifications
  • Building an iTunes-notification observer

In chapter 4, you learned a core fundamental of Cocoa development: delegation. But sometimes creating your own delegates won’t work. This is where notifications come in. Delegates are important and are used throughout Cocoa, but delegates assign one object as the delegate object—this single object is responsible for responding to an event. What if you need multiple objects to respond to the same event? That’s when you want to use notifications.

By the end of this chapter, you’ll be a notification ninja. We’ll explore what makes up notifications, how to register with and use observers, and how to send information with a notification. With a good understanding of notifications you’ll be well equipped to create a notification observer tool that displays the iTunes tracks that are playing, paused, and so on.

Before we dive into some code, let’s discuss why you might need notifications in a MacRuby application.

5.1. Notifying multiple objects

5.2. Setting up notifications

5.3. Queuing notifications

5.4. Responding to notifications

5.5. Building an iTunes-notification observer

5.6. Summary