iOS UINavigationController与UITabBarController的组合使用
2015-09-09 10:40
465 查看
1.导航类型
UINavigationController 适用于父子页面的跳转
UITabBarController 适用于平级页面的跳转
2.presentViewController / dismissViewControllerAnimated和pushViewController / popViewController
(1)只有从UINavigationController导航过来的UIViewController 才可以使用pushViewController / popViewController,从其它导航进入的只能使用presentViewController / dismissViewControllerAnimated
(2)如果A界面是通过presentViewController进入的,那么在A界面里只能使用presentViewController导航,如果A界面是通过UINavigationController包装了一层进入,则可使用pushViewController导航。
(3)如果A界面是一个UITabBarController,进入时是通过presentViewController来导航的,那么子Tab里也只能通过presentViewController来导航,如果进入时是通过UINavigationController包装了一层进入,则可使用[self.tabBarController.navigationController pushViewController:abcdViewController animated:true];这种方式导航。
3.修改UINavigationController导航进入后导航栏的图标及文字
UIBarButtonItem* leftBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(homeBack)];
self.navigationItem.leftBarButtonItem = leftBar;
//可添加多个导航按钮
// self.navigationItem.leftBarButtonItems = nil;
//同样右边的导航按钮也可以设置
4.创建UITabBarController时需注意,要先创建出来,然后通过UINavigationController包装后才能显示,否则tab子项不显示。创建格式如下
Java代码
TabOneViewController* oneVC = [[TabOneViewController alloc]init];
oneVC.view.backgroundColor = [UIColor whiteColor];
UITabBarItem* item1 = [[UITabBarItem alloc]initWithTitle:@"tab1" image:nil tag:0];
oneVC.tabBarItem = item1;
TabTwoViewController* twoVC = [[TabTwoViewController alloc]init];
UITabBarItem* item2 = [[UITabBarItem alloc]initWithTitle:@"tab2" image:nil tag:1];
twoVC.tabBarItem = item2;
HomeViewController* homeVC = [[HomeViewController alloc]init];
NSArray* array = [[NSArray alloc]initWithObjects:oneVC,twoVC, nil];
homeVC.viewControllers = array;
UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:homeVC];
[self presentViewController:navVC animated:YES completion:^{}];
//下面这种创建了HomeViewController后,HomeViewController下的子Tab不能通过push或者pop导航
// HomeViewController* homeVC = [[HomeViewController alloc]init];
// [self presentViewController:homeVC animated:YES completion:^{}];
5.UITabBarController各页面之间的切换
实现UITabBarControllerDelegate协议
参考文章:http://www.tuicool.com/articles/J7nIVbN
UINavigationController 适用于父子页面的跳转
UITabBarController 适用于平级页面的跳转
2.presentViewController / dismissViewControllerAnimated和pushViewController / popViewController
(1)只有从UINavigationController导航过来的UIViewController 才可以使用pushViewController / popViewController,从其它导航进入的只能使用presentViewController / dismissViewControllerAnimated
(2)如果A界面是通过presentViewController进入的,那么在A界面里只能使用presentViewController导航,如果A界面是通过UINavigationController包装了一层进入,则可使用pushViewController导航。
(3)如果A界面是一个UITabBarController,进入时是通过presentViewController来导航的,那么子Tab里也只能通过presentViewController来导航,如果进入时是通过UINavigationController包装了一层进入,则可使用[self.tabBarController.navigationController pushViewController:abcdViewController animated:true];这种方式导航。
3.修改UINavigationController导航进入后导航栏的图标及文字
UIBarButtonItem* leftBar = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(homeBack)];
self.navigationItem.leftBarButtonItem = leftBar;
//可添加多个导航按钮
// self.navigationItem.leftBarButtonItems = nil;
//同样右边的导航按钮也可以设置
4.创建UITabBarController时需注意,要先创建出来,然后通过UINavigationController包装后才能显示,否则tab子项不显示。创建格式如下
Java代码
TabOneViewController* oneVC = [[TabOneViewController alloc]init];
oneVC.view.backgroundColor = [UIColor whiteColor];
UITabBarItem* item1 = [[UITabBarItem alloc]initWithTitle:@"tab1" image:nil tag:0];
oneVC.tabBarItem = item1;
TabTwoViewController* twoVC = [[TabTwoViewController alloc]init];
UITabBarItem* item2 = [[UITabBarItem alloc]initWithTitle:@"tab2" image:nil tag:1];
twoVC.tabBarItem = item2;
HomeViewController* homeVC = [[HomeViewController alloc]init];
NSArray* array = [[NSArray alloc]initWithObjects:oneVC,twoVC, nil];
homeVC.viewControllers = array;
UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:homeVC];
[self presentViewController:navVC animated:YES completion:^{}];
//下面这种创建了HomeViewController后,HomeViewController下的子Tab不能通过push或者pop导航
// HomeViewController* homeVC = [[HomeViewController alloc]init];
// [self presentViewController:homeVC animated:YES completion:^{}];
TabOneViewController* oneVC = [[TabOneViewController alloc]init]; oneVC.view.backgroundColor = [UIColor whiteColor]; UITabBarItem* item1 = [[UITabBarItem alloc]initWithTitle:@"tab1" image:nil tag:0]; oneVC.tabBarItem = item1; TabTwoViewController* twoVC = [[TabTwoViewController alloc]init]; UITabBarItem* item2 = [[UITabBarItem alloc]initWithTitle:@"tab2" image:nil tag:1]; twoVC.tabBarItem = item2; HomeViewController* homeVC = [[HomeViewController alloc]init]; NSArray* array = [[NSArray alloc]initWithObjects:oneVC,twoVC, nil]; homeVC.viewControllers = array; UINavigationController* navVC = [[UINavigationController alloc]initWithRootViewController:homeVC]; [self presentViewController:navVC animated:YES completion:^{}]; //下面这种创建了HomeViewController后,HomeViewController下的子Tab不能通过push或者pop导航 // HomeViewController* homeVC = [[HomeViewController alloc]init]; // [self presentViewController:homeVC animated:YES completion:^{}];
5.UITabBarController各页面之间的切换
实现UITabBarControllerDelegate协议
//实现协议方法,用于切换Tab时,更改页面的标题 -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSInteger index = tabBarController.selectedIndex; NSString *title; switch (index) { case 0: title = @"Message"; break; case 1: title = @"User List"; break; } self.title
参考文章:http://www.tuicool.com/articles/J7nIVbN
相关文章推荐
- 开源项目分析之UIL(续)
- UICollectionView 高级进阶篇
- Java build模式
- iOS 提交代码出现提示弹出框显示 “A commit message is required to perform this operation.Enter a commit message and try again.“
- JSP页面中的标签以及内置对象request等
- 解决pycharm无法提示pyqt4.QtGui但是可以运行的问题
- 关于easyui的datagrid detailview的初次点击出现的重叠错位问题
- EasyUI基础知识Draggable(拖累)
- 关于使用xcconfig定制化build设置
- SharePoint 2013 Installation and Configuration Issues
- 【Android】 ProcessBuild Process su 命令
- UIView Animation 与 CACoreAnimation
- [Android fix] java.exe finished with non-zero exit value 2
- UINavigationController 返回上一级,返回根控制器,返回某一级
- iOS开发-------选择器(UIPickView)
- 如何使用Xcode分析调试在真机运行的UE4 IOS版游戏
- UI:页面传值、单例模式传值、属性传值、NSUserDefaults 数据持久化
- juicer
- 根据字符串长度计算UILabel的高度
- iOS:分组的表格视图UITableView,可以折叠和展开