Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Show modal UITableViewController with Navigation bar

I am showing a modal view which is a UITableViewController class. For some reason it won't show the navigation bar when I show it. Here is my code:

SettingsCreateAccount *detailViewController = [[SettingsCreateAccount alloc] initWithStyle:UITableViewStyleGrouped];     detailViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;     detailViewController.navigationController.navigationBarHidden = NO;     [self.navigationController presentModalViewController:detailViewController animated:YES];     detailViewController = nil;     [detailViewController release]; 

I thought it was shown by default? If it helps, I am calling this from another class that is also a UITableViewController managed by a UINavigationController. Ideas?

like image 229
Nic Hubbard Avatar asked Jan 05 '11 02:01

Nic Hubbard


2 Answers

When you present a modal view controller it does not use any existing navigation controllers or navigation bars. If all you want is to display a navigation bar, you need to add the navigation bar as a subview of your modal view and present it as you're doing.

If you want to present a modal view controller with navigation functionality, you need to present a modal navigation controller containing your detail view controller instead, like so:

SettingsCreateAccount *detailViewController = [[SettingsCreateAccount alloc] initWithStyle:UITableViewStyleGrouped]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:detailViewController]; [detailViewController release];  navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentModalViewController:navController animated:YES]; [navController release]; 

Your modal controller will manage its own navigation stack.

like image 168
BoltClock Avatar answered Nov 06 '22 17:11

BoltClock


Here is one way to display navigation bar for those who are using storyboards, suggested by Apple's Tutorial on Storyboard.

Because a modal view controller doesn’t get added to the navigation stack, it doesn’t get a navigation bar from the table view controller’s navigation controller. To give the view controller a navigation bar when presented modally, embed it in its own navigation controller.

  1. In the outline view, select View Controller.
  2. With the view controller selected, choose Editor > Embed In > Navigation Controller.
like image 33
Scott Avatar answered Nov 06 '22 19:11

Scott