views:

1906

answers:

3

I'm current creating a UISegmentedControl programmatically in a view controller's viewDidLoad method and adding it to the view controller's navigation bar by assigning it to self.navigationItem.titleView.

That's easy enough, but I'd like to be able to do this in Interface Builder as well and so far haven't been able to figure out how. Google hasn't been much help either. Can someone describe how to do this in IB or point to an online example? I'd be much appreciative. Thanks, Howard

A: 

You can't set the titleView property in IB, but you may be able to create / set up the control as a child of your controller's view via Interface Builder, and then in your viewDidLoad method, remove it from your view and set it as the titleView:

[segControl removeFromSuperview];
self.navigationItem.titleView = segControl;
Jason
That's an interesting idea -- creating something in IB, leaving it as a subview of something else just to make sure its accessible later on, and then grabbing the pointer and fixing up the view hierarchy in viewDidLoad. There's also an interesting little note in the documentation that removeFromSuperview releases the receiver, so that you need to retain it first before doing that (and releasing it again once you assign it to titleView -- no, I take that back: I would assume that the titleView property has retain semantics and will do the release for you).
hkatz
+1  A: 

If you've got whole nav stack in the nib, it's actually pretty easy; you can just drag it into the title area and IB does the right thing automatically.

Otherwise, you can just add the segmented control to the nib (not necessarily a subview) and then add an @property IBOutlet to it from your view controller. Then in viewDidLoad, assign it to the titleView as normal. Remember to release in dealloc, and you're golden.

Andrew Pouliot
Kudos on the second paragraph, Andrew: I like that even better. A question tho: I don't understand what you're saying in paragraph one. Drag *what* exactly into the title area?
hkatz
He means if you have a navigation based application, in the main window.nib you can drag a SegmentedControl into there, that will persist for the view stack.
KiwiBastard
+1  A: 

In IB you certainly can just drag a view into the middle of the navigation controller and it will work fine if its just inside one navigation item.

However, if the same view object reference is dragged into the title view area of different navigation items that will at some point be pushed onto the navigation controllers stack, you will run into problems with the title view disappearing when you travel back through the stack. The navigation controller isn't too happy with references to the same object popping up on multiple navigation items for some reason and it only throws a fit when you pop back to the view with the troublesome navigation item.

To get around this you MUST explicitly set and unset the titleView object when the you navigate to the views using the shared title view object reference. For instance, if you had custom logic behind a subclassed view set as the titleView that you only wanted to instantiate once.

Jeff Procsal