Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a UINavigationController as the detail view of a UISplitViewController?

I'm running into a problem with an iPad app where I would like to have UINavigationControllers in both of the views within a UISplitView. I've looked through other similar questions here, but most link to a tutorial online that doesn't completely solve the problem. Here's a 2-minute walkthrough to re-create the problem I'm having:

  1. Create a New Project in XCode, starting from the Split View-based Application template.
  2. Add the following NSLog statement as the first line within the DetailViewController's willHideViewController method:

    NSLog(@"toolbar: %@", toolbar);

If you run the application now, the log will show that the DetailViewController's toolbar is alive and well. Now...

  1. Open MainWindow.xib and expand the SplitViewController.
  2. Drag a Navigation Controller from the library on top of the DetailViewController.
  3. Expand the new Navigation Controller and change the class of the UIViewController within to a DetailViewController.
  4. Ctrl-drag from the SplitViewController to the DetailViewController and assign it as the delegate.
  5. Save MainWindow.xib and run the app again.

At this point, the detail view has a navigation bar and an empty toolbar. If you view the logs, you should find that the toolbar is null. Why is this? Am I missing some sort of connection in Interface Builder? Is the navigation bar the problem for some reason?

Unlike the tutorial at http://www.cimgf.com/2010/05/24/fixing-the-uisplitviewcontroller-template/, I would like to keep both the navigation bar and the toolbar (preferably with the toolbar at the top when in portrait and not visible when in landscape), so that I still have a functional "Back" button when the iPad is in portrait orientation.

Does anyone have any suggestions for fixing this problem? An example project with this sort of set-up would be ideal.

like image 313
B Sweigard Avatar asked Sep 21 '10 17:09

B Sweigard


1 Answers

You can certainly use a navigation controller on the detail view of a split view controller. In fact, the iPad Settings app uses this approach. Probably the best way to get this setup is to create a new project in Xcode 4.x and select the "Master-Detail Application" template. It will generate a split view controller with 2 navigation controllers, one for the left view and one for the right view.

To your toolbar question, to keep things simple I would put a toolbar in the bottom. You can still put bar button items on the top navigation bar, although you can only put them in the left, middle, or right. If you need lots of items on the top bar, one way is to add a toolbar to the detail view and hide the navigation bar in the viewWillAppear event of the detail view class.

Here is an example on how to hide the navigation bar and show the toolbar:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = NO;
    self.navigationController.navigationBarHidden = YES;
}
like image 200
xcoder Avatar answered Sep 23 '22 18:09

xcoder