« November 2010 | Main | January 2011 »

December 8, 2010

Altering Back Button in iOS (iPhone/iPad) Navigation Control

I've run into this enough times that I've got to jot it down (apologies for getting all technical). A few times when building iPhone apps I've needed to modify the button that takes you back one screen in an iOS view controller. On the iPhone this is done with the UIViewController object, which is a combination of views and controls for those views.

CocoaTouch provides nifty hooks to make moving between screens easy, but hides much of the navigation magic in a way that you don't get much control. If you're on a view and push a new view onto it with a control to go back, the back button is there automatically with a label matching the title of the previous screen. It's not terribly straightforward to modify the back button, so here are a few snips of code.

Change the UINavigationController Back Button Word/Title

In the calling view.

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Go Back!"
style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

Turn off the UINavigationController Back Button

In the displayed view where you want the back button disabled:

self.navigationItem.hidesBackButton = YES;

Now I won't have to dig and dig next time I come across one of these needs.

Posted by mike at 4:27 PM