Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Alternatively, you could store the UISegmentedControl designed in IB in it's own NIB. Then set the FileOwner to the viewcontroller class that will be using the segmentedControl instance. In the viewcontroller class, declare the segmentedcontrol as an IBOutlet Property and link it to the instance in the nib.

All left to using the designed instance is then to call:

[[NSBundle mainBundle] loadNibNamed:@"TTCustomSegmentedControl"
                              owner:self
                            options:nil];
self.navigationItem.titleView = sortSegmentControl;    

Just try this (works for me):

UISegmentedControl *mSegmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:
                                             @"Segment 1",
                                             @"Segment 2",
                                             nil]];

mSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
mSegmentedControl.tintColor = [UIColor redColor];

[mSegmentedControl setSelectedSegmentIndex:0];


[mSegmentedControl addTarget:self action:@selector(sectionPress:) 
           forControlEvents:UIControlEventValueChanged];

self.navigationItem.titleView = mSegmentedControl;

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top