Cocoa Touch: Setting UINavigationController's

2019-07-29 14:19发布

问题:

I've used [[self navigationController] setTitle:@"Test Title"] to no avail. This is the same way I do it in the rest of my app. What could cause this?

回答1:

Try setting the title of the navigation item.

self.navigationItem.title = @"Test Title";

Or like this if you prefer

[[self navigationItem] setTitle:@"Test Title"];


回答2:

You are using the title property incorrectly, the navigationController has a title property because it inherets from UIViewController, the title property is used by NavigationControllers to display a title, so if you wanted the title you gave your NavigationController to show you would need to present it in another NavigationCOntroller...But what you need to do, is set the viewControllers that you are displaying titles instead of the NavigationController, now whenever u display that VC youll see the title in the navigation bar... In short...the viewcontrollers title property is used by its navigationController to display the title on the navigation bar when that viewcontroller is on the top of the navigation stack...



回答3:

CustomViewController *viewController = [[CustomViewController alloc] init];
[viewController setTitle:@"CustomViewController!";
[custonNavigationController pushViewController:viewController animated:YES];

This is a simplification of what Daniel had said. This method properly follows stack protocol.



回答4:

SecondViewController *s1=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
[s1 setTitle:@"Second Page"];
[self pushViewController:s1 animated:YES];

Worked for me.