The iPhone Navigation Bar offers a simple way to add button-based navigation to your iPhone applications. Defined in UIKit/UINavigationBar.h, this class allows you to add one or two buttons to the kind of blue iPhone-looking bar shown here. Use these buttons to move between program screens or to add direct functionality to your application.
Here is the code for a very simple application that makes use of the Navigation Bar by placing two buttons on the top of the screen. Tap the buttons to update the displayed text. The source for this application includes:
To use a navigation bar, you must allocate it and initialize it with a frame. The frame defines the boundaries the bar will occupy in your window. The standard frame for one- or two-button navigation bars is 320 pixels wide by 48 pixels high frame based at the top-left of the iPhone window.
nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)]; The showButtonsWithLeftTitle: rightTitle: leftBack: method sets the number and kinds of buttons. Set leftBack: to YES or NO to specify whether the left button uses the back-pointing shape you see for many iPhone applications. Pass standard NSStrings to each title or use nil to omit the button from the display. e.g.
e.g. [nav showButtonsWithLeftTitle: @"Hello" rightTitle: @"Bye" leftBack: NO]; produces the two buttons shown here.
[nav showButtonsWithLeftTitle: @"Back" rightTitle: nil leftBack: YES]; shows a single back-styled Back button.
You can set the names on your buttons at any time as well as update their appearance. For example, you can jump between the two presentations examples I just listed above without penality.
Make sure to set the navigation bar's delegate to your main application to be able to receive the navigationBar: buttonClicked: message. The button number (0 or 1) tells you which item was tapped. In this sample, the text updates (goodbye or hello) based on the clicked button.













Reader Comments (Page 1 of 1)
8-16-2007 @ 1:24PM
Will said...
Thank you! I've been working on a couple of apps, but Objective C and Cocoa are strange and new to me. Please keep these kind of posts coming.
Reply
8-16-2007 @ 2:44PM
Patrick McCarron said...
What Will said! I got a game in progress, and I spend more time on figuring the little things out in the little time I have. These posts are very educational.
Reply
8-16-2007 @ 3:21PM
jtbandes said...
Awesome... Have you figured out how to make the nice transitions with the "back"-style buttons?
As a side note, is there a -showButonsWithLeftTitle:rightTitle:rightForward: method?
Reply
8-16-2007 @ 3:38PM
manny said...
Wow, great article, this really helped me
Reply
8-16-2007 @ 4:52PM
Rick Fillion said...
*$%^&! I want an iPhone now more than ever. COME ON ROGERS, ANNOUNCE THE DAMN THING!
Reply
8-16-2007 @ 7:14PM
michas_pi said...
In before "Enough of the iPhone news, TUAW!!!!!1111"
Reply
8-17-2007 @ 2:17AM
John said...
I have a short piece on my blog explaining how to use the regular UIPushButton class, and its derivatives, too for those playing with coding apps for the iPhone.
Reply