简体   繁体   English

如何在iOS上创建标签栏?

[英]How to create a tab bar on iOS?

I need on my main view controller to have a tab bar with tabs to navigate to all my other controllers. 我需要在我的主视图控制器上有一个带标签的标签栏,以导航到我所有的其他控制器。 I just need the tab bar on this controller and when i get to another controller i just need to have a back button to go to the main controller. 我只需要这个控制器上的标签栏,当我到达另一个控制器时,我只需要一个后退按钮就可以进入主控制器。 Now i have some questions. 现在我有一些问题。 I created the tab bar in the main view controller and all the tabs with the text and images that i need. 我在主视图控制器中创建了选项卡栏,并在所有选项卡中创建了我需要的文本和图像。 However i see that i can only create IBOutlet for the tab bar and not IBActions for every tab(as i thought). 但是我看到我只能为标签栏创建IBOutlet,而不是为每个标签创建IBAction(正如我所想)。 So i created an IBOutlet and connected it to my tab bar. 所以我创建了一个IBOutlet并将其连接到我的标签栏。

How can i refer to every tab? 我如何参考每个标签?

If i can refer to every tab how is it possible to change the view controller when a tab is selected when i cant use an action about it?(I am not asking for the code to change controllers , i am asking for the place that i should put the code so that my application knows that this specific tab was pressed and has to change controller). 如果我可以参考每个标签,当我不能使用关于它的操作时,如何在选择选项卡时更改视图控制器?(我不是要求代码更改控制器,我要求的地方我应该放置代码,以便我的应用程序知道按下了这个特定的选项卡,并且必须更改控制器)。 Thank you for reading my post :D 感谢您阅读我的帖子:D

You can create a UITabBarController programmatically in applicationDidFinishLaunching and set it as the root view controller (or if you prefer, you can present it as a modal view). 您可以在applicationDidFinishLaunching以编程方式创建UITabBarController ,并将其设置为根视图控制器(或者,如果您愿意,可以将其显示为模态视图)。 Here is the minimal code to do it: 这是执行此操作的最小代码:

UITabBarController *tabBarController = [[UITabBar alloc] init];

UIViewController *controller1 = [[YourViewController alloc] init];
UIViewController *controller2 = [[YourOtherViewController alloc] init];

tabBarController.viewControllers = [NSArray arrayWithObjects:
    controller1,
    controller2,
    nil];

// set as the root window
window.rootViewController = tabBarController;

If you want to customize the look of the tab bar items, do so by adding overloading (UITabBarItem *)tabBarItem in the child view controller(s): 如果要自定义选项卡栏项的外观,请通过在子视图控制器中添加重载(UITabBarItem *)tabBarItem来执行此操作:

- (UITabBarItem *)tabBarItem
{
    return [[UITabBarItem alloc] initWithTitle:@"Amazing" image:[UIImage imageNamed:@"Blah.png"] tag:0];
}

How to make a tab bar controller by me 如何由我制作标签栏控制器

  1. Drag tab bar controller into storyboard (hopefully you have one) 将标签栏控制器拖动到故事板(希望你有一个)
  2. Control-drag from tab bar controller to each view you want hooked up to it 控制 - 从标签栏控制器拖动到您想要连接到它的每个视图
  3. Pop bottles 流行的瓶子

在此输入图像描述

Just so you know, this gives you the default tab bar controller behavior (so it will always be present and you can click from any page to another). 您知道,这为您提供了默认的标签栏控制器行为(因此它将始终存在,您可以从任何页面单击到另一个页面)。 If that's not what you want, then don't use a tab bar controller. 如果这不是您想要的,那么请不要使用标签栏控制器。 To do otherwise is an abomination. 否则是令人憎恶的。

Storyboards are definitely helpful, but if you don't want to use one that's fine. 故事板肯定是有帮助的,但如果你不想使用一个没问题。 Doing the Control Drag from the Tab Bar Controller to your new View Controller does indeed work (Dustin's response). 执行控制从标签栏控制器拖动到新的视图控制器确实有效(达斯汀的回应)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM