ios UINavigationController 如何设置标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3959003/
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
UINavigationController how to set title
提问by grilix
I have a Controller/View for a generic list of items, that can be extended for displaying a custom list.. Listing and navigation works fine.. but I can't change the title of UINavigationController. In the generic Controller:
我有一个通用项目列表的控制器/视图,可以扩展以显示自定义列表.. 列表和导航工作正常.. 但我无法更改 UINavigationController 的标题。在通用控制器:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview: navigationController.view];
}
- (void)setNavigationTitle: (NSString *)title
{
NSLog(@"set title: %@", title); // this works
self.navigationController.title = title; // Nothing works here
}
Then, the extended class does..
然后,扩展类做..
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNavigationTitle: @"Custom list"];
}
The navigationBar still have "Item" as title :(
导航栏仍然有“项目”作为标题:(
回答by willcodejavaforfood
In your UIViewController
there is a title
property and it is that property that will be displayed by the NavigationController
. So when pushing a new UIViewController
onto the navigation stack set the title of that UIViewController
to whatever is appropriate.
在您的中UIViewController
有一个title
属性,该属性将由NavigationController
. 因此,当将新内容推UIViewController
送到导航堆栈时,请将其标题设置UIViewController
为适当的值。
In your case it looks like it would be:
在您的情况下,它看起来像:
[self setTitle:@"WhateverTitle"];
回答by fiedl
For those looking for a Swift solution:
对于那些寻找 Swift 解决方案的人:
class CustomViewController: SuperViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "My Custom Title"
}
}
The title
needs to be set on the UIViewController
and not on the UINavigationController
embedding the view controller.
的title
需要被所设定的UIViewController
,而不是在UINavigationController
嵌入视图控制器。
Documentation: UIViewController
Class Reference: title
Property
回答by Gyani
use
用
self.title = @"yourTitle";
回答by SwiftArchitect
Swift
迅速
title = "whateverTitle"
. It's a UIViewController
instance property ; invoke anytime.
title = "whateverTitle"
. 这是一个UIViewController
实例属性;随时调用。
From the documentation:
从文档:
Declaration
var title: String? { get set }
Discussion
Set the title to a human-readable string that describes the view. If the view controller has a valid navigation item or tab-bar item, assigning a value to this property updates the title text of those objects.
宣言
var title: String? { get set }
讨论
将标题设置为描述视图的人类可读字符串。如果视图控制器具有有效的导航项或选项卡栏项,则为该属性赋值会更新这些对象的标题文本。
回答by infiniteLoop
I know its old thread but thought of sharing this Set the 'self.title' in 'init' method in UIVIewControler derived class, This worked for me!!
我知道它的旧线程,但想到在 UIVIewControler 派生类中的“init”方法中设置“self.title”,这对我有用!