如何以编程方式将navigationController添加到tabBar Controller?

时间:2012-02-24 12:01:26

标签: iphone

在AppDelegate.m中,我正在编写以下导航栏代码。

MaterialsListViewController *materials = [[MaterialsListViewController alloc]initWithNibName:@"MaterialsListView" bundle:nil];
    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:materials];

在同一个AppDelegate.m中,我正在编写以下tabbar代码。

UIViewController *viewController1, *viewController2;
viewController1 = [[[MaterialsListViewController alloc] initWithNibName:@"MaterialsList" bundle:nil] autorelease];
viewController2 = [[[RecentMaterialsListViewController alloc] initWithNibName:@"RecentMaterialsList" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
NSArray *items = self.tabBarController.tabBar.items;
UITabBarItem *item1 = [items objectAtIndex:0];
[item1 setTitle:@"Materials"];
UITabBarItem *item2 = [items objectAtIndex:1];
[item2 setTitle:@"Recent"];
self.window.rootViewController = self.tabBarController;

如何将NavigationBar添加到标签栏?

2 个答案:

答案 0 :(得分:3)

您错过了UINavigationController

创建viewController1

使用您的viewController初始化UINavigationController具有RootViewController:

UINavigationController *navigationController_01 = [[UINavigationController alloc] initWithRootViewController:(UIViewController *)];

然后将navigationController_01添加到tabBarController.viewControllers

self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController_01, navigationController_02, nil];

答案 1 :(得分:1)

在下面的行中,您可以将navigationcontroller作为数组的成员传递。

self.tabBarController.viewControllers = [NSArray arrayWithObjects: navigation, viewController2, nil];