UINavigationController如何设置标题
我有一个用于通用项目列表的控制器/视图,可以扩展它以显示自定义列表。列表和导航工作正常。但我无法更改 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
}
然后,扩展类会..
- (void)viewDidLoad
{
[super viewDidLoad];
[self setNavigationTitle: @"Custom list"];
}
导航栏仍然以“Item”作为标题:(
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:
- (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 :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在
UIViewController
中有一个title
属性,该属性将由NavigationController
显示。因此,当将新的UIViewController
推送到导航堆栈时,将该UIViewController
的标题设置为适当的内容。在你的情况下,它看起来会是:
In your
UIViewController
there is atitle
property and it is that property that will be displayed by theNavigationController
. So when pushing a newUIViewController
onto the navigation stack set the title of thatUIViewController
to whatever is appropriate.In your case it looks like it would be:
对于那些寻找 Swift 解决方案的人:
需要在
UIViewController
上设置title
,而不是在嵌入视图控制器的UINavigationController
上设置。文档:
UIViewController
类参考:title
属性For those looking for a Swift solution:
The
title
needs to be set on theUIViewController
and not on theUINavigationController
embedding the view controller.Documentation:
UIViewController
Class Reference:title
Property使用
use
Swift
title = "whateverTitle"
.它是一个UIViewController
实例属性;随时调用。来自文档:
Swift
title = "whateverTitle"
. It's aUIViewController
instance property ; invoke anytime.From the documentation:
我知道它是旧线程,但想分享这个
在 UIVIewControler 派生类的 'init' 方法中设置 'self.title',
这对我有用!
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!!