ios 设置在界面生成器中创建的导航栏的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4670907/
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
Set the title of Navigation Bar created in Interface Builder
提问by Steve
I have a modal view controller whose view comes from a XIB. I want it to have the same look and feel as the rest of my navigation based app, but it's not a view that's pushed to the navigation stack, it's presented modally. I dragged a UINavigationBar in and made it an outlet, but it doesn't have a title property to set. I have two fields that can bring up this modal view, and I want the title set differently depending on which one creates the modal view.
我有一个模式视图控制器,其视图来自 XIB。我希望它与我的基于导航的应用程序的其余部分具有相同的外观和感觉,但它不是推送到导航堆栈的视图,它以模态呈现。我拖了一个 UINavigationBar 并使它成为一个插座,但它没有要设置的 title 属性。我有两个字段可以调出这个模态视图,我希望根据哪个字段创建模态视图来设置不同的标题。
回答by codelark
UINavigationBar
's manage a stack of UINavigationItem
s much like a UINavigationController
manager a stack of UIViewController
s. To set what is visible directly, you should use either pushNavigationItem:animated:
or setItems:animated:
using the navigationItem of the view controller you want the bar to reflect.
UINavigationBar
管理一堆UINavigationItem
s 就像UINavigationController
管理一堆UIViewController
s 一样。要设置直接可见的内容,您应该使用pushNavigationItem:animated:
或setItems:animated:
使用您希望栏反映的视图控制器的 navigationItem。
eg:
例如:
self.navigationItem.title = @"A custom title";
[self.navigationBar pushNavigationItem:self.navigationItem animated:NO];
The above code where you have a property navigationBar
which references the stand-alone navigation bar.
上面的代码有一个navigationBar
引用独立导航栏的属性。
If you don't want to manage it yourself, you can do as mplappert suggested and nest your view controller (without a stand-alone UINavigationBar
) in a UINavigationController
and present the navigation controller modally instead of your view controller.
如果您不想自己管理它,您可以按照 mplappert 的建议进行操作,并将您的视图控制器(没有独立的UINavigationBar
)嵌套在 a 中,UINavigationController
并以模态呈现导航控制器而不是您的视图控制器。
回答by typeoneerror
Also found this easier way if you've already defined a UINavigationBar in your IB. Assuming self.navigationBar is your IBOutlet:
如果您已经在 IB 中定义了 UINavigationBar,还可以找到这种更简单的方法。假设 self.navigationBar 是您的 IBOutlet:
self.navigationBar.topItem.title = title;
回答by user1864957
(Using XCode 4.5.2) Found a method that works directly in Interface Builder - without setting in code.
(使用 XCode 4.5.2)找到了一种直接在 Interface Builder 中工作的方法 - 无需在代码中进行设置。
With the ViewController object selected in (IB), under the Identity Inspector there is a section labeled "User Defined Runtime Attributes". I added the keyPath "title" of type String and set it to the value I wanted.
在 (IB) 中选择 ViewController 对象后,在 Identity Inspector 下有一个标记为“用户定义的运行时属性”的部分。我添加了 String 类型的 keyPath“标题”并将其设置为我想要的值。
Tested and this works when pushed onto a Nav Controller stack and when opened modally (with a Modal Nav Controller of course).
测试,这个作品时推到导航控制器堆栈,并且当模态打开(当然是一个模态导航控制器)。
回答by yogesh yadav
viewcontroller.h file in create iboutlet of the navigation item
导航项的创建 iboutlet 中的 viewcontroller.h 文件
@property (weak, nonatomic) IBOutlet UINavigationItem *navigationBarTitle;
- Named it "navigationBarTitle"
- 将其命名为“navigationBarTitle”
in viewcontroller.m file in add the code in super viewDidLoad
在 viewcontroller.m 文件中添加 super viewDidLoad 中的代码
self.navigationBarTitle.title = @"Favorites";
connect the storybord in navigation bar item to navigationBarTitle.
将导航栏项目中的 storybord 连接到 navigationBarTitle。
回答by Jouhar
Here is the Simplest way:
这是最简单的方法:
[[[self.navigationController navigationBar] topItem] setTitle:@"The Title"];
回答by ShaileshAher
you can use existing navigation bar with following code
您可以使用以下代码使用现有的导航栏
self.navigationController?.navigationBar.topItem?.title = "Calender"
回答by Devine Linvega
in my .h file
在我的 .h 文件中
Dragged the Child of the navigation item from the storyboard into the .h file
Named it "navigationBarTitle"
将导航项的子项从故事板拖到 .h 文件中
将其命名为“navigationBarTitle”
in my .m file
在我的 .m 文件中
- Added self.navigationBarTitle.title = @"Some Title";
- 添加 self.navigationBarTitle.title = @"Some Title";
回答by mplappert
Simply put your UIViewController
into a new UINavigationController
s view controller stack and present that navigation controller modally.
只需将您的视图控制器堆栈UIViewController
放入一个新UINavigationController
的视图控制器堆栈中,并以模态方式呈现该导航控制器。