将UITabBarController添加到NavigationController

时间:2015-07-18 15:04:02

标签: ios objective-c uinavigationcontroller uitabbarcontroller

我的应用程序目前有一个UINavigationController,我想在完成登录过程后的某个时刻推送一个mainTabBarController(子类UITabBarController)。 我试图在Interface Builder上创建它(而不是以编程方式)。

我的问题是mainTabBarController似乎被推到UINavigationController上,所以我可以看到底部的所有标签栏,但是,我没有看到屏幕上应该有的任何视图元素。我只看到黑屏和标签栏。

如果在mainTabBarController中嵌入的每个viewcontrollers上的右侧位置都有视图元素,例如标签和按钮,我会进行双重检查。

我跟着this tutorial并仔细阅读了this文章,但仍然无法找到错误的内容。

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];

编辑:我用下面的代码解决了这个问题。坦率地说,我仍然不明白为什么我不能使用上面的代码。希望这对某人有所帮助。

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];

1 个答案:

答案 0 :(得分:1)

为了实现你想要的,我认为它应该是这样的:

//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];