HowTo: Add a Custom UINavigationItem to a UINavigationController with delegation

Posted by James Brindle on May 20, 2011.

In a project i’ve been working on, on the iPad version, my client wants a screen with a Map and then the ability to type in what to look for on the top bar, a bit like the Safari Web Browser app but without the animating search box sizes.

He also wants to have an alternate screen on a different tab which has a list or some icons which are categories, from this the user can tap a category and it will switch to the map tab and fill in the search boxes and start the search off.

This sounds simple doesn’t it? Yes! Can’t I just draw the searchbox on the screen and set the text of it? NO!

The problem, right or wrong is how Apple have set up the UINavigationController (for the layman, that grey bar at the top of the screen which you’re used to where there’s a list or a map or anything where tapping on an item usually slides the other view in from the right but more on that in a moment.

The UINavigationController can only cater for 3 inbuilt properties which let you control the things that go on the navigation bar; the Left Button (usually taken up with a back button), the title and the right button.

This is true even on the iPad with it’s bigger screen space and anybody who has used one will know that people make use of the extra space for things like settings buttons and search boxes – what I couldn’t find was documentation o nthe apple website that explains how you do it.

Firstly, the three properties we’re talking about within UINavigationController’s navigationItem property are ‘leftBarButtonItem’, ‘titleView’ and ‘rightBarButtonItem’ – it’s the latter that i’m talking about in this case.

The issue I was having was that because everything happens in different threads, you don’t reliably know if the control has been loaded or not when it’s part of some custom view and therefore, if you write to it’s properties, your code may not crash but everything is out of sequence and things get reverted back to their default values when they hit the screen.

The good news is that leftBarButtonItem and rightBarButtonItem accept a UIBarButtonItem object rather than just some text and as UIBarButtonItem returns a view, we can do some customising of our own and the way we do this is with an initialiser method called ‘initWithCustomView’.

Given that information, we know that classes with a user interface can have a view object and that means we can make our own custom subclass and not only that, we can harness objective-c and the iPhone’s methodology of using protocols and delegation.

This means that (for those familiar with iPhone development), you get a viewDidLoad: message which is what you need to be able to sort out the sequence of events for when the controls are actually initialised and can be modified.

On the next page i’ll walk you through a brief example that you should be able to code yourself and at the end, there’s a download link for a zip file containing an example project you can derive this from.

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.