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

心已入冬 提交于 2019-12-06 11:34:30

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2492621/how-to-add-a-tab-bar-to-an-existing-view-controller-without-xib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!