IPhone: 设置导航栏标题

大家好。我对 iPhone 的开发还是相当新鲜的,而且我在弄清楚如何更改导航栏的标题时遇到了一点麻烦。关于这个网站上的另一个问题,有人建议使用:

viewController.title = @"title text";

但是这对我来说不起作用... ... 我需要添加一个 UINavigationController 来完成这个操作吗?或者也许只是我的 UIViewController 子类的一个插座?如果有帮助的话,我在 IB 中定义了导航栏,并尝试在我的 UIViewController 子类中设置它的标题。这是另一件让我头疼的简单事情。在 viewDidLoadinitWithNibName中放入 self. title =@“ title text”也不起作用。有人知道发生了什么,以及如何让它正常发生吗?

谢谢!

147189 次浏览

if you are doing it all by code in the viewDidLoad method of the UIViewController you should only add self.title = @"title text";

something like this:

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"title";
}

you could also try self.navigationItem.title = @"title";

also check if your navigationItem is not null and if you have set a custom background to the navigationbar check if the title is set without it.

I guess you need a dynamic title that is why you don't set it in IB.

And I presume your viewController object is the one specified in the NIB?

Perhaps trying setting it to a dummy value in IB and then debug the methods to see which controller has the dummy value - assuming it appears as the title...

The view controller must be a child of some UINavigationController for the .title property to take effect. If the UINavigationBar is simply a view, you need to push a navigation item containing the title, or modify the last navigation item:

UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];

or

bar.topItem.title = @"title text";

In my navigation based app I do this:

myViewController.navigationItem.title = @"MyTitle";

There's one issue with using self.title = @"title";

If you're using Navigation Bar along with Tab bar, the above line also changes the label for the Tab Bar Item. To avoid this, use what @testing suggested

self.navigationItem.title = @"MyTitle";

By default the navigation controller displays the title of the 'topitem'

so in your viewdidload method of your appdelegate you can. I tested it and it works

navController.navigationBar.topItem.title = @"Test";
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];

This code worked.

I had a navigation controllers integrated in a TabbarController. This worked

self.navigationItem.title=@"title";

If you are working with Storyboards, you can click on the controller, switch to the properties tab, and set the title text there.

If you want to change the title of a navBar inside a tabBar controller, do this:

-(void)viewDidAppear:(BOOL)animated {
self.navigationController.navigationBar.topItem.title = @"myTitle";
}

If you want to change navbar title (not navbar back button title!) this code will be work.

self.navigationController.topViewController.title = @"info";

From within your TableViewController.m :

self.navigationController.navigationBar.topItem.title = @"Blah blah Some Amazing title";

For all your Swift-ers out there, this worked perfectly for me. It's notably one of the shorter ways to accomplish setting the title, as well:

override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "presentLineItem" {
print("Setting Title")
var vc = segue.destinationViewController as! LineItemsTableViewController
vc.navigationItem.title = "Line Item"
}
}