views:

374

answers:

1

Hi,

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  A: 

I dont 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 wont 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, its 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.

medopal
Thanks :) I've added it to the view, it's appearing, with a blank canvas and an empty bar at the bottom.Now I'm trying to figure out how to tell the tab bar to show a specific view on the white canvas and how to add 4 buttons to the empty tab bar at the bottom.
Andre
@Andres, updating answer
medopal
I've added my specific view to the white canvas. I'm using: tabBarController = [[UITabBarController alloc] init]; [tabBarController setViewControllers: [NSArray arrayWithObjects: viewController, nil]]; viewController.view = step3; where "step3" is the view I wanted to show. Now I'm just trying to add the actual tab buttons and associate actions to change the views
Andre
right, I've got the buttons in. any idea how to get each button to call a function to change the view? :)cheers
Andre
i added the buttons by using: UINavigationController *tab = [[UINavigationController alloc] initWithRootViewController:viewController]; pushing that into an array and then setting the tabBarController.viewControllers with that array. The thing is it's automatically adding a navigation bar on the top. Is it because I'm using the UINavigationController to create buttons for the Tab Bar? How can I manipulate this nav bar? Using "tabBarController.navigationController.navigationBar" doesn't work :(
Andre
found that one, finally. now just need to link buttons to actions :) any thoughts?
Andre
still don't know how to track the Tab change, but thanks for the rest :D
Andre
I'm not sure i can help further, it should work right away, anyway, check the last update, i added a link to a lecture that might help
medopal