xcode 如何在没有 XIB 的情况下将 Tab Bar 添加到现有的视图控制器

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

How to add a Tab Bar to an existing view controller, without XIB

iphonexcodeuitabbarcontroller

提问by Andre

I'm trying to avoid using Interface Builderas much as possible.

尽量避免使用 Interface Builder

At the moment I have the view controller created via code and change views via code as well.

目前我通过代码创建了视图控制器,并通过代码更改视图。

I now need one of the steps to send the app intoa new view with a tab bar, that will allow me to change views as well.

我现在需要其中一个步骤来将应用程序发送到带有标签栏的新视图中,这也将允许我更改视图。

Ideally, what I'd do is tell the current view controller to add a Tab Bar to the bottom, but I'm not sure if that's doable, so I might have to swap the UIViewController with a UITabBarController?

理想情况下,我要做的是告诉当前视图控制器在底部添加一个 Tab Bar,但我不确定这是否可行,所以我可能需要将 UIViewController 与 UITabBarController 交换?

Any help will be appreciated.

任何帮助将不胜感激。

Cheers, Andre

干杯,安德烈

回答by medopal

I don't have Xcode on hand so I will try to answer verbally.

我手头没有 Xcode,所以我会尝试口头回答。

Create a new UITabBarControllerand set your current view as the root view, then add as much tabs as you want, (each tab has its own view).

创建一个新UITabBarController视图并将当前视图设置为root view,然后根据需要添加尽可能多的选项卡(每个选项卡都有自己的视图)。

UPDATE
After init'ing the controller, define an array of views (order of adding is important). And call this on the tab bar controller

更新
初始化控制器后,定义一个视图数组(添加顺序很重要)。并在标签栏控制器上调用它

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

UPDATE 2

更新 2

Here is a simple code to create a tab bar with two empty views, each has its own tab button.

这是一个简单的代码,用于创建一个带有两个空视图的标签栏,每个视图都有自己的标签按钮。

tabBarController = [[UITabBarController alloc]init];

firstView = [[FirstView alloc] init];
UITabBarItem *item1 = [[[UITabBarItem alloc]initWithTitle:@"First" image:nil tag:1] autorelease];
[firstView setTabBarItem:item1];

secondView = [[SecondView alloc] init];
UITabBarItem *item2 = [[[UITabBarItem alloc]initWithTitle:@"Sec" image:nil tag:1] autorelease];
[secondView setTabBarItem:item2];

[tabBarController setViewControllers:[NSArray arrayWithObjects:firstView,secondView,nil] animated:NO];

[window addSubview:tabBarController.view];

Of course this code won't be useful as is, you will need to create the views manually, or create a nib file for each view and load it in initWithNibName

当然,此代码不会按原样使用,您需要手动创建视图,或者为每个视图创建一个 nib 文件并将其加载到 initWithNibName

UPDATE 3
Check this Stanford iPhone Course, it's a free course from Stanford univ. the lecturers are Apple employees. Lecture 7 titled Navigation & Tab Bar Controllerswill give you a good start on those components.

更新 3
查看此斯坦福 iPhone 课程,这是斯坦福大学的免费课程。讲师是苹果公司员工。第 7 课标题为“导航和标签栏控制器”将为您提供这些组件的良好开端。