Wednesday, August 22, 2012

Adding UITabBarController in an application with UINavigationController for iOS without StoryBoard in Xcode

Some people are finding some difficulties to add UITabBarController to a UINavigation based application and since I, myself was in the same situation until I managed to find a solution after spending couple of time coding, I would like to share this to you.


Firstly, create a new project in Xcode with simple view based application and add a UINavigationController (which I guess most of you already know else you can see my previous post http://ioscoderef.blogspot.com/2012/08/adding-uinavigation-to-simple-view.html ).
Then from which view you want to navigate to the UITabBarViewController there you add this code


//create a UITabBarController object

UITabBarController *tabBarController=[[UITabBarController alloc]init];

//FirstViewController and SecondViewController are the view controller you want on your UITabBarController

FirstViewController *firstViewController=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];

SecondViewController *secondViewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

//adding view controllers to your tabBarController bundling them in an array
tabBarController.viewControllers=[NSArray arrayWithObjects:firstViewController,secondViewController, nil];

//navigating to or presenting the UITabBarController that you created
[self.navigationController pushViewController:tabBarController animated:YES];
or
[self presentModalViewController:tabBarController animated:YES];


Then you are good to go :D

Suggestions and corrections are most welcome. If you have any please do without hesitation. Leave your thoughts and comments. 

No comments:

Post a Comment