Vincent Gigandet
Vincent Gigandet

Reputation: 948

Adding UITabBarController to NavigationController

My app currently has a UINavigationController and I'd like to push a mainTabBarController(subclassed UITabBarController) at some point when finished login process. I am trying to create it on Interface Builder (as opposed to programmatically).

My issue is that mainTabBarController seems to be pushed on the UINavigationController so I can see all the tab bars at the bottom, however, I don't see any view elements on the screen which there should be. I see only black screen and tab bars.

I doublechecked if there are view elements such as labels and buttons on the right place on each viewcontrollers that are embedded in mainTabBarController.

I followed this tutorial and read this article carefully but still can't find what's wrong.

MainTabBarController *mainTabBarVC = [MainTabBarController new];

// Create child VCs for tabBarController
self.feedVC = [FeedViewController new];  
self.chatVC = [ChatViewController new];
self.friendsVC = [FriendsViewController new];
self.meVC = [MeViewController new];

self.feedVC.tabBarItem.title = @"Feed";
self.chatVC.tabBarItem.title = @"Chat";
self.friendsVC.tabBarItem.title = @"Friends";
self.meVC.tabBarItem.title = @"Me";

mainTabBarVC.viewControllers = @[self.feedVC, self.chatVC, self.friendsVC, self.meVC];

[self.navigationController pushViewController:mainTabBarVC animated:YES];

EDIT : I solved this issue with below code. Frankly, I still don't get it why I couldn't with above code. Hope this help somebody.

self.feedVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FeedVC"];
self.chatVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ChatVC"];
self.friendsVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FriendsVC"];
self.meVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MeVC"];

self.feedVC.tabBarItem.title = @"Feed";
self.chatVC.tabBarItem.title = @"Chat";
self.friendsVC.tabBarItem.title = @"Friends";
self.meVC.tabBarItem.title = @"Me";

mainTabBarVC.viewControllers = @[self.feedVC, self.chatVC, self.friendsVC, self.meVC];

[self.navigationController pushViewController:mainTabBarVC animated:YES];

Upvotes: 0

Views: 97

Answers (1)

SanitLee
SanitLee

Reputation: 1253

To achieve what you want, I think it should look something like this:

//create a UITabBarController object
UITabBarController *tabBarController=[[UITabBarController alloc]init];

// Create child VCs for tabBarController
FeedViewController *feedVC=[[FeedViewController alloc]initWithNibName:@"feedVC" bundle:nil];
ChatViewController *chatVC=[[ChatViewController alloc]initWithNibName:@"chatVC" bundle:nil];

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

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

Upvotes: 1

Related Questions