views:

32

answers:

2

Hello,

I am wondering why my UINavigationController is not letting me set a title. It lets me change the tint of the navigationBar, but the title and rightBarButtonItem I set just get ignored. Why?

Here's my code:

  taps = 0;

  UIView*controllerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

  controllerView.backgroundColor = [UIColor whiteColor];

  controller = [[UIViewController alloc] init];

  [controller setView:controllerView];

  [controllerView release];

  navController = [[UINavigationController alloc] initWithRootViewController:controller];

  navController.navigationBar.barStyle = 1;

  navController.navigationItem.title = @"Setup";

  UIBarButtonItem*item = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonSystemItemDone target:self action:@selector(dismissSetup:)];

  navController.navigationItem.rightBarButtonItem = item;

  [item release];

  [self presentModalViewController:navController animated:YES];

  [mp stop];

p.s: I know i'm not releasing some of the stuff I alloc'ed, I do that later!

A: 

The navigation controller updates the middle of the navigation bar as follows: If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item. If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this label is usually obtained from the title property of the view controller itself. If you want to display a different title than the one associated with the

view controller, set the title property of the view controller’s navigation item instead.

You want your UINavigationController to change its title depending on which viewcontroller is currently on top. So the way to go is to set the title in the viewcontrollers you are pushing onto the viewControllers array.

And by the way: I don't think navigationcontrollers are supposed to be presented modally:

Because the UINavigationController class inherits from the UIViewController class, navigation controllers have their own view that is accessible through the view property. When deploying a navigation interface, you must install this view as the root of whatever view hierarchy you are creating. For example, if you are deploying the navigation interface by itself, you would make this view the main subview of your window. To install a navigation interface inside a tab bar interface, you would install the navigation controller’s view as the root view of the appropriate tab.

Joseph Tura
how do I set the view Controllers title?
David Schiefer
+3  A: 

set: self.title = @"This is my title"; in the viewController

(or in your case set controller.title = @"this is my title";)

Thomas Clayson