objective-c iPhone:设置导航栏标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2280710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone: Setting Navigation Bar Title
提问by Arthur Skirvin
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 :
大家好。我对 iPhone 开发还是很陌生,我在弄清楚如何更改导航栏的标题时遇到了一些麻烦。在本网站上的另一个问题上,有人建议使用:
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 viewDidLoadand initWithNibNamedidn't work either. Anybody know what's happening and how to get it happening right?
但这对我不起作用......我需要添加一个 UINavigationController 来完成这个吗?或者也许只是我 UIViewController 子类的一个出口?如果有帮助,我在 IB 中定义了导航栏,并尝试在我的 UIViewController 子类中设置其标题。这是另一件让我头疼的简单事情。把 self.title = @"title text"; 在viewDidLoad,initWithNibName也没有工作。有谁知道发生了什么以及如何让它正确发生?
Thanks!
谢谢!
回答by kennytm
The view controller must be a child of some UINavigationController for the .titleproperty 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:
视图控制器必须是某个 UINavigationController 的子级,.title属性才能生效。如果 UINavigationBar 只是一个视图,则需要推送一个包含标题的导航项,或者修改最后一个导航项:
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];
or
或者
bar.topItem.title = @"title text";
回答by Sorin Antohi
if you are doing it all by code in the viewDidLoadmethod of the UIViewControlleryou should only add self.title = @"title text";
如果您在viewDidLoad方法中通过代码完成所有操作,UIViewController则只应添加self.title = @"title text";
something like this:
像这样:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"title";
}
you could also try self.navigationItem.title = @"title";
你也可以试试 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.
还要检查您的 navigationItem 是否不为空,以及您是否为导航栏设置了自定义背景,检查标题是否设置为没有它。
回答by Jigish
There's one issue with using self.title = @"title";
使用有一个问题 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
如果您将导航栏与标签栏一起使用,上面的行还会更改标签栏项目的标签。为避免这种情况,请使用@testing 建议的内容
self.navigationItem.title = @"MyTitle";
回答by M.G?kay Borulday
If you want to change navbar title (not navbar back button title!) this code will be work.
如果您想更改导航栏标题(不是导航栏后退按钮标题!),此代码将起作用。
self.navigationController.topViewController.title = @"info";
回答by Pantelis Proios
If you want to change the title of a navBar inside a tabBar controller, do this:
如果要更改 tabBar 控制器内导航栏的标题,请执行以下操作:
-(void)viewDidAppear:(BOOL)animated {
self.navigationController.navigationBar.topItem.title = @"myTitle";
}
回答by testing
In my navigation based app I do this:
在我的基于导航的应用程序中,我这样做:
myViewController.navigationItem.title = @"MyTitle";
回答by ronaldus_magnus
By default the navigation controller displays the title of the 'topitem'
默认情况下,导航控制器显示“topitem”的标题
so in your viewdidload method of your appdelegate you can. I tested it and it works
所以在你的 appdelegate 的 viewdidload 方法中,你可以。我测试了它并且它有效
navController.navigationBar.topItem.title = @"Test";
回答by Sagar Dalwale
I had a navigation controllers integrated in a TabbarController. This worked
我在 TabbarController 中集成了一个导航控制器。这有效
self.navigationItem.title=@"title";
回答by Guy
If you are working with Storyboards, you can click on the controller, switch to the properties tab, and set the title text there.
如果您正在使用 Storyboard,您可以单击控制器,切换到属性选项卡,然后在那里设置标题文本。
回答by rmsh
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"title text"];
...
[bar pushNavigationItem:item animated:YES];
[item release];
This code worked.
此代码有效。

