UINavigationController如何设置标题

发布于 2024-09-27 18:12:56 字数 558 浏览 8 评论 0原文

我有一个用于通用项目列表的控制器/视图,可以扩展它以显示自定义列表。列表和导航工作正常。但我无法更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等风来 2024-10-04 18:12:56

UIViewController 中有一个 title 属性,该属性将由 NavigationController 显示。因此,当将新的 UIViewController 推送到导航堆栈时,将该 UIViewController 的标题设置为适当的内容。

在你的情况下,它看起来会是:

[self setTitle:@"WhateverTitle"];

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.

In your case it looks like it would be:

[self setTitle:@"WhateverTitle"];
苍景流年 2024-10-04 18:12:56

对于那些寻找 Swift 解决方案的人:

class CustomViewController: SuperViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "My Custom Title"
  }
}

需要在 UIViewController 上设置 title,而不是在嵌入视图控制器的 UINavigationController 上设置。

文档: UIViewController 类参考:title 属性

For those looking for a Swift solution:

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 UINavigationControllerembedding the view controller.

Documentation: UIViewController Class Reference: title Property

极致的悲 2024-10-04 18:12:56

使用

self.title = @"yourTitle";

use

self.title = @"yourTitle";
耳钉梦 2024-10-04 18:12:56

Swift

title = "whateverTitle".它是一个 UIViewController 实例属性;随时调用。

来自文档

声明
var 标题:字符串? { 获取设置 }

讨论
将标题设置为描述视图的人类可读字符串。如果视图控制器具有有效的导航项或选项卡栏项,则为此属性分配值会更新这些对象的标题文本。

Swift

title = "whateverTitle". It's a UIViewController instance property ; invoke anytime.

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.

左耳近心 2024-10-04 18:12:56

我知道它是旧线程,但想分享这个
在 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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文