在uitabbarcontroller中更改选项卡名称 [英] Change tab name in uitabbarcontroller

查看:279
本文介绍了在uitabbarcontroller中更改选项卡名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TabBarController设置为主控制器选项卡,其中名称是使用界面构建器定义的。现在我想以编程方式更改其名称。

I have a TabBarController set as main controller tab, where names were defined using interface builder. Now I would like to change its names programmatically.

如何实现?

推荐答案

更新到XCode 8



由于我原来的答案,很多事情发生了:Swift 3,Storyboards等。标题通常是所有视图共享的,可以在属性检查器

Updated to XCode 8

Since my original answer, a lot has happened: Swift 3, Storyboards, etc. Title is usually the one that all views share, and can be set in the attributes inspector

此外,为了更多控制,您可以随时拖动设计器视图中的 UITabBarItem UINavigationItem 元素。您必须将它们拖动到要在标签栏/导航控制器中显示的视图。基本上他们将信息存储为我想在标签栏中显示这样。

Also, for more control, you can always drag the UITabBarItem, and UINavigationItem elements from the designer view. You must drag them to the view that's gonna be displayed in the tab bar/navigation controller. Basically they store the info as "I wanna be displayed in tab bar like this".


Hello Scene正在添加一个TabBarItem,因此可以进行配置。

Hello Scene is getting a TabBarItem added to it, so it can be configured.

这两个元素的行为与通过代码访问视图控制器的 .tabBarItem .navigationItem 。这些属性总是存在于代码中,如果它们是相应对象的子对象(nav有navItem,tab有tabItem),你不需要在storyboard / xib中添加它们。

These two elements behave exactly as accessing the view controller's .tabBarItem and .navigationItem properties via code. These properties always exist in code if they are child of the corresponding object (nav has navItem, and tab has tabItem), you don't need to add them in storyboard/xib to have them.

最后一件事情有点令人困惑,因为在storyboard / xib中,你会将它们添加到视图控制器,但实际上你只是说nib也将配置这些属性。

This last thing is kinda confusing, since in the storyboard/xib it appears you're adding them to the view controller, but in truth you're just saying "the nib will configure these properties too".

标签栏上显示的名称来自 UIViewController ' title 属性,

The name that appears on the tab bar comes from the UIViewController's title property,

self.title = @"Name me!";

您可以随时更改标题,并且应该更新标签栏项目上显示的文本。但是要小心,尽快执行此操作,最好是在使用中的 init 方法中(或 initWithNibName:bundle : initWithCoder:)。

You can change the title at any point, and it should update the text appearing on the tab bar item. But be wary, do this as soon as possible, ideally, in the init method in use (or initWithNibName:bundle:, or initWithCoder:).

方法在屏幕上出现选项卡栏时立即调用,因为它会初始化其所有视图控制器。如果你在 viewDidLoad 上这样做,那么只有当你实际选择标签,然后选择其他系列调用时,才会被调用, awakeFromNib viewWillAppear: viewDidAppear:等。

The key here, is that the init methods are called as soon as the tab bar appears on screen, as it initialises all of its view controller. If you were to do it on viewDidLoad, that would only get called if you actually select the tab, then other family of calls, same goes for awakeFromNib, viewWillAppear:, viewDidAppear:, etc.

UIViewController 上有一个标题的想法是保持一致。如果你在 UINavigationController 上显示viewController,顶部的导航栏应该使用title属性,就像它在使用时一样。 UITabBarController 也尊重相同的标题属性并相应地改变。

The idea of having a title on the UIViewController, is to keep things consistent. If you show that viewController on a UINavigationController, the navigation bar on top should use the title property, as it does when using back. The UITabBarController also respects the same title property and changes accordingly.

在可重用性方面,

In terms of reusability, you should be setting the title only from the inside of the UIViewController subclass.

使用笔尖或故事板?如果你有一个 UIViewController ,你可以直接在属性检查器(或 < kbd> 4 )

Using nibs or storyboards? If you have a UIViewController, you can give it the name straight up in the attributes inspector (or 4)

不幸的是,如果文件所有者是 UIViewController 子类, t能够以这种方式访问​​它,只是因为,XCode注册文件所有者作为外部对象,并没有显示它的配置面板。 :(

Unfortunately, if the File Owner is the UIViewController subclass, then you won't be able to access it this way, simply because, XCode registers the File Owner as an "External Object", and doesn't show a configuration panel for it. :(

但有时候, / p>

But sometimes, you just want to have them named differently

// Modify the display title on the tab bar
self.tabBarItem.title = @"World";

// Modify the display title on the navigation bar
self.navigationItem.title = @"Hello World";



与邻居进行调整



每个UIViewController都应该处理自己的名称,如果你想从外部强制

Screwing with the neighbours

Each UIViewController should handle his own name, what if you want to force it from the outside (and thus, completely violating the original thing I said about reusability)?

// Rename the other guy's .title property
[[self.tabBarController.viewControllers objectAtIndex:<#Index#>] setTitle:@"Hello World"];

// Or do as before, and rename the other guy's tab bar 
[(UITabBarItem*)[self.tabBarController.tabBar.items objectAtIndex:<#index#>] setTitle:@"Hello World"];

您也可以使用导航项目,但这将需要更多的体操比我舒服。

You could also probably do it with the navigation item, but that would require more gymnastics than I'm comfortable with.

这篇关于在uitabbarcontroller中更改选项卡名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆