Massively brings you complete coverage from the Warhammer Online beta!

iPhone Coding: Working with the Navigation Bar

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)

TUAW Features

back-to-school
Mac 101 ask-tuaw
Mac News
WWDC (251)
.Mac (66)
Accessories (652)
Airport (75)
Analysis / Opinion (1424)
Apple (1698)
Apple Corporate (580)
Apple Financial (200)
Apple History (51)
Apple Professional (54)
Apple TV (164)
Audio (450)
Bad Apple (131)
Beta Beat (155)
Blogging (87)
Bluetooth (19)
Bugs/Recalls (57)
Cult of Mac (879)
Deals (225)
Desktops (116)
Developer (282)
Education (110)
eMac (10)
Enterprise (147)
Features (412)
Freeware (399)
Gaming (397)
Graphic Design (38)
Hardware (1308)
Holidays (37)
Humor (589)
iBook (66)
iLife (240)
iMac (185)
Internet (340)
Internet Tools (1342)
iTS (982)
iTunes (823)
iWork (23)
Leopard (376)
Mac mini (112)
Mac Pro (54)
MacBook (206)
MacBook Air (83)
Macbook Pro (226)
MobileMe (47)
Multimedia (459)
Odds and ends (1489)
Open Source (282)
OS (940)
Peripherals (214)
Podcasting (183)
Podcasts (95)
Portables (198)
PowerBook (136)
PowerMac G5 (51)
Retail (612)
Retro Mac (50)
Rig of the Week (42)
Rumors (642)
Software (4459)
Software Update (428)
Steve Jobs (254)
Stocking Stuffers (50)
Surveys and Polls (98)
Switchers (114)
The Woz (35)
TUAW Business (257)
Universal Binary (281)
UNIX / BSD (61)
Video (908)
Weekend Review (84)
WIN Business (47)
Wireless (89)
Xserve (39)
iPhone/iPod News
iPhone (1789)
iPod Family (2118)
App Store (158)
SDK (29)
Mac Events
One More Thing (27)
Liveblog (2)
Other Events (226)
Macworld (489)
Mac Learning
AppleScript (4)
Ask TUAW (107)
Blogs (85)
Books (26)
Books and Blogs (62)
Cool tools (451)
Hacks (472)
How-tos (490)
Interviews (44)
Mods (191)
Productivity (591)
Reviews (114)
Security (167)
Terminal Tips (66)
Tips and tricks (575)
Troubleshooting (173)
TUAW Features
iPhone 101 (36)
TUAW Labs (4)
Blast From the Past (19)
TUAW Tips (150)
Flickr Find (38)
Found Footage (90)
Mac 101 (111)
TUAW Interview (31)
Widget Watch (198)
The Daily Best (1)
TUAW Faceoff (6)

RESOURCES

RSS NEWSFEEDS

Powered by Blogsmith

Sponsored Links

The Unofficial Apple Weblog (TUAW) bloggers (30 days)

#BloggerPostsCmts
1Cory Bohon551
2Robert Palmer5339
3Steven Sande4915
4Mike Schramm250
5Erica Sadun231
6Michael Rose2328
7Dave Caolo210
8Giles Turnbull190
9Mat Lu188
10Christina Warren1328
11Brett Terpstra120
12TUAW Blogger70
13Victor Agreda, Jr.512
14Scott McNulty10
15Jason Clarke11

Featured Galleries

DNC Macs
Macworld 2008 Keynote
Macworld 2008 Build-up
Apple Vanity Plates
DiscPainter
Crash Bandicoot Nitro Kart 3D
Macworld Expo 2007 show floor
Apple Texas Hold 'Em
The Macworld Faithful in Line

 

    Most Commented On (7 days)

    Recent Comments

    More Apple Analysis

    More from AOL Money and Finance

    Other Weblogs Inc. Network blogs you might be interested in: