HowTo: Add a Custom UINavigationItem to a UINavigationController with delegation

Posted by James Brindle on May 20, 2011.

In the template Navigation Based Application, you get a MainWindow.xib with a UINavigationController and a RootViewController which you can then add things to, feel free to put a label or two on the root view controller’s nib layout. This tutorial isn’t designed to build an app, it’s to show a concept at work.

Edit RootViewController.h as follows:

Add #import “CustomNavBar.h” at the top and alter the @interface line so it looks similar to:

[objc gutter="false"]@interface RootViewController : UITableViewController <CustomNavBarDelegate>[/objc]

Edit RootViewController.m as follows, first find the viewDidLoad method and add the following:

[objc gutter="false"]
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Hello!";
CustomNavBar *customNav = [[CustomNavBar alloc] initWithDelegate:self];
UIBarButtonItem *customButton = [[UIBarButtonItem alloc] initWithCustomView:customNav.view];
self.navigationItem.rightBarButtonItem = customButton.view;
[customButton release];
[customNav release];
}
[/objc]

Then add this method which implements the message receiver from CustomNavBar:

[objc gutter="false"]
- (void)customNavBarIsReadyToBeConfigured:(CustomNavBar *)navBar {
navBar.searchWhat.text = @"Our Text";
}
[/objc]

Compile and run it and that should be it, you’ll have an iPad app with a custom navigation bar with a search box and it’ll get configured by your code and it should look like this.

Screenshot of compiled CustomNavBar App

I’ve added code in the example to populate the table with some values just to make the screenshot look a bit prettier so don’t think you’ve done something wrong when you run your version and the table has nothing.

Some puritans will say that you could have done this in interface builder with one form if all you wanted was a search bar on the navigation bar but this concept can be extended to have other buttons, you just hook them up to IBAction’s in your interface and extend the delegate protocol so that your custom navigation class can message it’s calling class.

I’ve found that most things aren’t that hard to do on for the iPad/iPhone, they just take some understanding but look at it, in less than 50 lines of code (most of it was created for you by the way), we’ve implemented a cool feature just like the professionals do.

An archive with the sample code can be downloaded here: CustomNavBar.zip

Pages: 1 2 3

About the Author

James Brindle is a freelance technology consultant based in Blackburn, Lancashire with over 20 years of experience in the IT, Media and Telecommunications industries with specialties in Web and Mobile Application development, Voice over IP (VoIP) deployment and integration, media storage and distribution systems and wide area solutions. He has a passion for retro gaming and volunteers as a presenter and acts as vice-chairman of Blackburn's Hospital Radio service.

Share Our Posts

  • Delicious
  • Digg
  • Newsvine
  • RSS
  • StumbleUpon
  • Technorati

Comments

There are no comments on this entry.

Trackbacks

  1. iOS: Setting text in nib subview from view in UITabBar/UINavigationController application - Programmers Goodies

Add a Comment

You must be logged in to post a comment.