ios 在 uitabbarcontroller 中更改选项卡名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7250413/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:19:24  来源:igfitidea点击:

Change tab name in uitabbarcontroller

iosobjective-ciphonecocoa-touchuitabbarcontroller

提问by Ruth85

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.

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

How could it be done?

怎么可能呢?

回答by Can

Updated to XCode 8

更新到 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

自从我最初的回答以来,发生了很多事情:Swift 3、Storyboards 等。标题通常是所有视图共享的一个,可以在属性检查器中设置

Title Setup

标题设置

Also, for more control, you can always drag the UITabBarItem, and UINavigationItemelements 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".

此外,为了获得更多控制,您始终可以从设计器视图中拖动UITabBarItem、 和UINavigationItem元素。您必须将它们拖到将在选项卡栏/导航控制器中显示的视图。基本上他们将信息存储为“我想像这样显示在标签栏中”。

enter image description here

在此处输入图片说明

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

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

These two elements behave exactly as accessing the view controller's .tabBarItemand .navigationItemproperties 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.

这两个元素的行为.tabBarItem.navigationItem通过代码访问视图控制器和属性完全一样。如果这些属性是相应对象的子对象(nav 具有 navItem,而 tab 具有 tabItem),则这些属性始终存在于代码中,您无需将它们添加到 storyboard/xib 中即可拥有它们。

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".

最后一件事有点令人困惑,因为在故事板/xib 中,您似乎将它们添加到视图控制器中,但实际上您只是在说“笔尖也会配置这些属性”。

Original Answer

原答案

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

出现在标签栏上的名称来自UIViewControllertitle属性,

self.title = @"Name me!";

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 initmethod in use (or initWithNibName:bundle:, or initWithCoder:).

您可以随时更改标题,它应该更新出现在选项卡栏项目上的文本。但要小心,尽快执行此操作,理想情况下,在init正在使用的方法中(或initWithNibName:bundle:、 或initWithCoder:)。

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.

这里的关键是,一旦选项卡栏出现在屏幕上,就会调用 init 方法,因为它会初始化其所有视图控制器。如果你是这样做的viewDidLoad,这只会被调用,如果你真的选择选项卡,然后调用的其他家庭,同样适用于awakeFromNibviewWillAppear:viewDidAppear:,等。

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 UITabBarControlleralso respects the same title property and changes accordingly.

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

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

在可重用性方面,您应该仅从UIViewController子类内部设置标题。

The way of the Nib

笔尖之道

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

使用笔尖或故事板?如果您有UIViewController,则可以在属性检查器(或??4)中直接为其命名

Using interface builder, in the Attributes Inspector

在属性检查器中使用界面构建器

Unfortunately, if the File Owner is the UIViewControllersubclass, 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. :(

不幸的是,如果文件所有者是UIViewController子类,那么您将无法通过这种方式访问​​它,仅仅是因为 XCode 将文件所有者注册为“外部对象”,并且不显示其配置面板。:(

Multiple titles, same view controller

多个标题,相同的视图控制器

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";

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)?

每个 UIViewController 都应该处理自己的名字,如果你想从外部强制它怎么办(因此,完全违反了我说的关于可重用性的原始内容)?

// 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.

你也可以用导航项来做,但这需要比我更适应的体操。

回答by Bart?omiej Semańczyk

However it is possible to do it in code, it is better to set this directly in Storyboard. How?

但是可以在代码中进行,最好直接在Storyboard 中进行设置。如何?

Just tap the appropriate tab bar item inside controller(NOT INSIDE TABBAR CONTROLLER).

只需点击控制器内相应的标签栏项目(而不是标签栏控制器内)。

enter image description here

在此处输入图片说明

Then switch to attribute inspectoron the Utilitiespanel.

然后attribute inspector在“实用工具”面板上切换到。

enter image description here

在此处输入图片说明

Now you are able to change everything:-)

现在你可以改变一切了:-)

回答by Alfi

Swift 4+

斯威夫特 4+

tabBarController?.tabBar.items![1].title = "xx"

回答by Alfi

In your viewController.m

在你的viewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
  //Here you are setting title

self.title = NSLocalizedString(@"Title", @"Title");

}
return self;
}

回答by TommyG

This is how you can change the name and the image of the icon of a tab bar:

这是更改选项卡栏图标的名称和图像的方法:

 self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"Main Tab" image:[UIImage imageNamed:@"maintab.png"]] autorelease];

回答by RelativeGames

You probably want to do this in UITabBarController::viewDidLoad in which case you need to set the view controller's title because the tab bar items are currently 0 at that point. So use

您可能希望在 UITabBarController::viewDidLoad 中执行此操作,在这种情况下,您需要设置视图控制器的标题,因为此时选项卡栏项目当前为 0。所以用

[[self.viewControllers objectAtIndex:0] setTitle: @"Buga"];
[[self.viewControllers objectAtIndex:1] setTitle: @"Nuga"];

回答by Sebastián Lara

I'm using Xcode Version 8.2.1 (8C1002) and this works for me:

我正在使用 Xcode 版本 8.2.1 (8C1002),这对我有用:

enter image description here

在此处输入图片说明