Notifications

Notifications (Chapter 11) can be a way to communicate between objects that are conceptually distant from one another without bothering to provide any way for one to see the other. Using a notification in this way may seem lazy, an evasion of your responsibility to architect your objects sensibly. But sometimes one object doesn’t need to know, and indeed shouldn’t know, what object it is sending a message to.

I’ll give a specific example. One of my apps consists of a bunch of flashcards. Only one card is showing at any one time, but the cards are actually embedded in a scroll view, so the user can move from one card to the next by swiping the screen. I’ve supplied classes to manage this interface: each card is managed by a CardController, and the scroll view as a whole is managed by a single ScrollViewController.

The flashcards all have the same layout: a foreign term, along with an English translation. To facilitate learning, the ScrollViewController displays a toolbar with a button that the user can tap to toggle visibility of the English translation on all cards. Thus, the user could hide the English translation and move from card to card, showing the English translation only occasionally to discover a forgotten translation or to confirm a remembered one.

What should happen, exactly, when the user taps the toolbar button? This seems a perfect use of a notification. I could cycle through all the existing CardController instances and tell each one to show or hide the ...

Get Programming iOS 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.