将UITabBarController添加到UIViewController

我的应用程序目前有一个UINavigationController ,我想推一个UITabBarController在点击一个button时的某一点。 我试图在Interface Builder上创build它(而不是编程)。

所有的在线教程显示了如何创build一个基于标签栏的应用程序,其中涉及拖动一个UITabBarControllerMainWindow.xib显然不是我想要的。

我所做的是创build一个UIViewController和它的nib文件,拖动一个UITabBarController 。 现在将UIViewController推送到导航控制器将显示一个空视图(空视图)。 删除视图控制器中的视图将会使应用程序崩溃。 我如何告诉UIViewController加载一个UITabBarController而不是它自己的视图?

对于那些投降我的人来说,至less提供一个评论是不错的。 这个问题不是一个很差的问题。 问题是要求如何以非正统的方式使用UITabBarController的build议。 我尝试了大部分的build议,他们不工作。 如果你是低调投票,至less写评论。

你可以看到这可以帮助你

因为这是你想要你的应用程序的方式: – 导航控制器 – 根视图控制器 – 其他视图控制器 – 标签栏控制器 – 第一个VC下标签 – 第二个VC下标签 – 第三个VC下标签 – 更多的视图控制器

在你的视图控制器你想pushViewController到UITabBarController使用这个

 //create a UITabBarController object UITabBarController *tabBarController=[[UITabBarController alloc]init]; //FirstViewController and SecondViewController are the view controllers you want on your UITabBarController (Number of view controllers can be according to your need) 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 the UITabBarController that you created [self.navigationController pushViewController:tabBarController animated:YES]; 

本教程可能会有帮助。 它带有一个示例代码 。

嗨,只需在导航控制器和TabBar控制器在应用程序委托。 最初添加navController到你的根视图..

 [self.window addSubview:navigationController.view]; 

每当你想添加标签栏,然后删除navController并添加tabBarController。

 -(void)addTabBarController { AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate]; [[[appdelegte navigationController] view]removeFromSuperview]; [[appdelegte window]addSubview:[[appdelegte tabcontroller]view]]; [[appdelegte tabcontroller]setSelectedIndex:0]; } 

如果你有什么问题,再问我一次。

在YourView控制器中制作tabBarController的IBOutlet

在.h文件中

 #import <UIKit/UIKit.h> @interface YourView : UIViewController { IBOutlet UITabBarController *tabBarController; } -(IBAction)loadTabBar:(id)sender; @end 

和.m文件中

 #import "YourView.h" #import "FirstViewController.h" #import "SecondViewController.h" @implementation YourView -(IBAction)loadTabBar:(id)sender { FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; tabBarController = [[UITabBarController alloc] init]; tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil]; [self.navigationController pushViewController:tabBarController animated:YES]; } @end 

tabBarController IBOutlet必须连接到.xib文件上的UITabBarController 。 而那个带有两个视图控制器的UITabBarController命名为FirstViewControllerSecondViewController

我记得做了类似这样的事情…

我不得不创build一个自定义的UITableViewController来做到这一点,如果你打算使用UINavigationController来“推”它。

只有在界面构build器中才能做到这一点可能有点棘手,我已经有一段时间了,我记得做正确的事情有点噩梦。

问题是,正如我相信我已经提到的地方,是XIB没有连接到它的UIView。 当在XIB文件中删除UIView并添加了UITabBarController时,XIB的view属性必须连接到UITabBarController的视图。 我连接它,它的工作。 这就是我获得SIGTRAP的原因。

采取标签栏控制器uiview意味着创build一个界面生成器的标签,并添加该标签栏uivew在你想要的标签栏

例如,在该UI视图中使用3个选项卡的标签栏uiview,并使用界面生成器中的三个button

对于每个类的导航应该添加这个uview类

 -(IBAction)firt_button_pressed:(id)sender { } -(IBAction)second_button_pressed:(id)sender { }