如何在不使用XIB的情况下将选项卡栏添加到现有视图控制器 [英] How to add a Tab Bar to an existing view controller, without XIB

查看:40
本文介绍了如何在不使用XIB的情况下将选项卡栏添加到现有视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尽可能避免使用Interface Builder .

此刻,我已经通过代码创建了视图控制器,并且还通过代码更改了视图.

我现在需要执行以下步骤之一,以标签栏将应用程序发送到新视图,这也将允许我更改视图.

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

任何帮助将不胜感激.

干杯,安德烈(Andre)

解决方案

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

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

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

 -(void)setViewControllers:(NSArray *)viewControllers动画化:(BOOL)动画 

更新2

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

  tabBarController = [[UITabBarController alloc] init];firstView = [[FirstView alloc] init];UITabBarItem * item1 = [[[[UITabBarItem alloc] initWithTitle:@第一张图片:无标签:1]自动发布];[firstView setTabBarItem:item1];secondView = [[SecondView alloc] init];UITabBarItem * item2 = [[[[UITabBarItem alloc] initWithTitle:@"Sec" image:nil tag:1]自动发布];[secondView setTabBarItem:item2];[tabBarController setViewControllers:[NSArray arrayWithObjects:firstView,secondView,nil]动画:否];[window addSubview:tabBarController.view]; 

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

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

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

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 into a 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?

Any help will be appreciated.

Cheers, Andre

解决方案

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

Create a new UITabBarController and set your current view as the root view, then add as much tabs as you want, (each tab has its own 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

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

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 Controllers will give you a good start on those components.

这篇关于如何在不使用XIB的情况下将选项卡栏添加到现有视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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