Using IB to add a UISegmentedControl to a Navigati

2019-07-04 10:47发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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;    


回答4:

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;


回答5:

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;