现在的位置: 首页 > 综合 > 正文

TabBarController和NavigationController并存

2014年08月29日 ⁄ 综合 ⁄ 共 2627字 ⁄ 字号 评论关闭

TabBarController中需要使用NavigationController,这样可以实现TabbarController中的视图导航。我总结了三种方法去实现,以供大家参考。

第一种:最简单的是从NavigationController下手,先用TabBarController建立XIB文件,在XIB上拉出相应的Tabbar。这时如果去建立导航,只需要在上一页和下一页之间建立相应的对应关系。而如何建立对应关系呢,请看下面的代码:

EDUThreeViewController *th = [[EDUThreeViewController allocinitWithNibName:@"EDUThreeViewController" bundle:nil];

    UINavigationController *n = [[UINavigationController allocinitWithRootViewController:th];

[self presentViewController:n animated:YES completion:nil];

但我现在了解到这种仅支持弹出下一级视图。

第二种:也是先建立TabBarController的XIB,然后在TabBar上拖入NavigationController,(这里可以根据自己的需要的拖入NavigationControlle或TabBarItem,拖入NavigationController的页面就自动有NavigationBar了)。

如下图:

http://my.csdn.net/my/album/detail/1444381

就这样,你就可以在相应的视图中进行导航的设计了。但会在下一级视图中出现TabBar,如果不需要可以通过代码去除。所有代码如下:

EDUStuViewController *stuViewController = [[EDUStuViewController allocinit];

    stuViewController.hidesBottomBarWhenPushed = YES;//去掉TabBar

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

第三种:前面说的都是在XIB视图中的,这里我们要说的是在代码中实现的。这里需要在delegate中进行TabBarController和NavigationController的结合。实例代码如下:

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow allocinitWithFrame:[[UIScreen mainScreenbounds]];

    //Override point for customization after application launch.

    //TabBar上的第一个视图

EDUaaViewController *aa = [[EDUaaViewController allocinitWithNibName:@"EDUaaViewController" bundle:nil];

//把ViewController加入到NavigationController中

UINavigationController *aaNav = [[UINavigationController allocinitWithRootViewController:aa];

//TabBar上的第二个视图

EDUbbViewController *bb = [[EDUbbViewController allocinitWithNibName:@"EDUbbViewController" bundle:nil];

//把ViewController加入到NavigationController中

    UINavigationController *bbNav = [[UINavigationController allocinitWithRootViewController:bb];

    //把navigationController放入数组中

    NSArray *controllerArray = [[NSArray allocinitWithObjects:aaNav,bbNav,nil];

    //建立TabBarController,需要在.h中先声明

    tabBarController = [[UITabBarController allocinit];

    tabBarController.delegate = self;

    //把navigationController的数组加入到tabBarController中去

    tabBarController.viewControllers = controllerArray;

    tabBarController.selectedIndex = 0;

    [(UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:0setTitle:@"检数据"]; 

    [(UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:1setTitle:@"健康档案"];

    //UIViewController* activeController =tabBarController.selectedViewController;

    [self.window addSubview:tabBarController.view];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

抱歉!评论已关闭.