如何在UITabBarController中插入UINavigationController

如何在UITabBarController插入UINavigationController

目前我的主要UITabBarController与应用程序委托中的declatarion像这样(所以tab是主要的)

 self.window.rootViewController = self.tabBarController; 

在其中一个标签中我想插入UINavigationController ,但无法获取它。

代码构造如下:

  1. 带有UITabBarController对象的MainWindow.xib ,其选项卡类型为UINavigationController (指向NavigationHistory.xib ) – 屏幕截图: 无效链接
  2. NavigationHistory.xib仅包含UINavigationController ,其中视图指向History.xib
  3. History.xib只有UITableView元素 – 截图: 无效链接

现在UIViewController没有显示我的View1视图,我也不知道它为什么会这样。 也许你有任何线索? 或者指向我进行此类配置的地方。

我会自己回答。 在https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html上解释得很好。

在appdelegate.m文件中写一个代码…..

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; NSMutableArray *Mutablearray = [[NSMutableArray alloc] init]; UIViewController *1st_View = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil]; UINavigationController *Navigation = [[UINavigationController alloc] initWithRootViewController:1st_View]; [Mutablarray addObject:Navigation]; UIViewController *2nd_View = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil]; UINavigationController *Navigation = [[UINavigationController alloc] initWithRootViewController:2nd_View]; [Mutablearray addObject:Navigation]; self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.viewControllers = Mutablearray; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES; } 

将控制器对象添加到UINavigationController,并将该navigationcontroller对象添加到UITabBarController

UINavigationController是UIViewController的子类,UITabBarController需要一个UIViewControllers数组(因此UINavigationController); 这告诉我,我可以给UITabBarController一个UINavigationControllers(或其子类)数组,给出所需的交互。

参考: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITabBarController_Class/

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UITabBarController *tab = [[UITabBarController alloc] init]; SimpleTableViewController *tableView = [[SimpleTableViewController alloc] init]; UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tableView]; UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:0]; nav1.tabBarItem = item; AboutViewController *about = [[AboutViewController alloc] init]; UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:about]; UITabBarItem *item2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0]; nav2.tabBarItem = item2; tab.viewControllers = @[nav1,nav2]; self.window.rootViewController = tab; [self.window makeKeyAndVisible]; return YES; 

}