Navigation Controllers

A navigation bar (UINavigationBar, see also Chapter 25) is a horizontal bar displaying, at its simplest, a center title and a right button. When the user taps the right button, the navigation bar animates, sliding its interface out to the left and replacing it with a new interface that enters from the right, displaying a back button at the left side, and a new center title — and possibly a new right button. Thus the user can now either go further forward (to the right), tapping the right button to proceed to yet another center title, or else go back (to the left), tapping the back button to return to the first center title and the first right button.

There’s a computer science name for the architecture I’m describing — a stack. Conceptually, a navigation bar represents a stack. Under the hood, a navigation bar really is a stack. A navigation bar holds an internal stack of navigation items (UINavigationItem). It starts out with one navigation item (the root or bottom item); you can then push another navigation item onto the stack, and from there you can either pop that navigation item to remove it from the stack or push yet another navigation item onto the stack.

At any moment, therefore, some navigation item is the top item on the stack, the most recently pushed item still present on the stack (the topItem). Furthermore, unless the top item is also the root item (because it is the only item in the stack), some navigation item is the back item (the backItem

Get Programming iOS 6, 3rd Edition 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.