iPhone: Setting Navigation Bar Title

2019-01-08 05:29发布

Hey all. I'm still pretty new to iPhone development, and I'm having a bit of trouble figuring out how to change the title of my Navigation Bar. On another question on this site somebody recommended using :

viewController.title = @"title text";

but that isn't working for me...Do I need to add a UINavigationController to accomplish this? Or maybe just an outlet from my UIViewController subclass? If it helps, I defined the navigation bar in IB and I'm trying to set its title in my UIViewController subclass. This is another one of those simple things that gives me a headache. Putting self.title = @"title text"; in viewDidLoad and initWithNibName didn't work either. Anybody know what's happening and how to get it happening right?

Thanks!

14条回答
狗以群分
2楼-- · 2019-01-08 05:59

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";
}
查看更多
We Are One
3楼-- · 2019-01-08 06:01
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];

This code worked.

查看更多
贼婆χ
4楼-- · 2019-01-08 06:02

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

self.navigationController.topViewController.title = @"info";
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-08 06:02

From within your TableViewController.m :

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

查看更多
▲ chillily
6楼-- · 2019-01-08 06:03

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

self.navigationItem.title=@"title";
查看更多
走好不送
7楼-- · 2019-01-08 06:04

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...

查看更多
登录 后发表回答