How to add a Tab Bar to an existing view controller, without XIB
I'm trying to avoid using Interface Builder as much as possible.
At the moment I have the view con开发者_如何学JAVAtroller 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.
精彩评论