iOS中多控制器的使用
通常情况下,一个app由多个控制器组成,当app中有多个控制器的时候,我们就需要对这些控制器进行管理。
在开发过程中,当有多个View时,可以用一个大的view去管理多个小的view,控制器也是如此,可以用一个控制器去管理多个控制器。
比如,用一个控制器A去管理3个控制器B、C、D,则控制器A是控制器B、C、D的父控制器,控制器B、C、D是控制器A的子控制器。
iOS中提供了2个比较特殊的控制器,可以用来管理多个子控制器,分别是:
UINavigationController 和 UITabBarController。
一: UINavigationController 的简单介绍
UINavigationController 在app 中很常见,其最典型的应用是系统自带的 "设置",如下图:

点击"通用",跳到General 控制器,点击“键盘”,跳转到“键盘”控制器,点击左上角的按钮,能够返回到上一个控制器。
UINavigationController 是通过栈的形式来管理子控制器。越先进入栈的控制器,越靠后被弹出。
UINavigationController 使用 push 方法将某个控制器压入栈:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
使用pop方法可以移除控制器,回到某一个控制器有三种情况,分别如下:
(1)将栈顶控制器移除
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
(2)回到指定的子控制器
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
(3)回到根控制器(栈底控制器)
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
在使用 UINavigationController时,导航栏的内容由栈顶控制器的 navigationItem的属性决定。
UINavigationItem 有以下属性影响着导航栏的内容:
(1)左上角的返回按钮
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;
(2)中间的标题视图
@property (nonatomic,retain) UIView *titleView;
(3)中间的标题文字
@property (nonatomic,copy) NSString *title;
(4)左上角的视图
@property (nonatomic,retain) UIBarButtonItem *leftBarButtonItem;
(5)右上角的视图
@property (nonatomic,retain) UIBarButtonItem *rightBarButtonItem;
示例程序:
初始化UINavigationController 并将其设置为窗口的 rootViewController。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
WMNOneViewController *one = [[WMNOneViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:one];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
UINavigationItem的一些属性使用:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.frame = [[UIScreen mainScreen] bounds];
//设置title
self.navigationItem.title = @"第一个控制器";
//设置下一个控制器的返回按钮
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
//设置当前(栈顶)控制器的左上角视图
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.frame = [[UIScreen mainScreen] bounds];
//设置中间的标题视图
self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
//设置当前控制器右上角的视图
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.frame = [[UIScreen mainScreen] bounds];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
//设置当前(栈顶)控制器的右上角视图
self.navigationItem.rightBarButtonItems = @[item1,item2];
}
跳转到下一个控制器,示例程序如下:
- (IBAction)jumpTwo {
//跳转到下一个控制器
WMNTwoViewController *two = [[WMNTwoViewController alloc] init];
[self.navigationController pushViewController:two animated:YES];
}
返回到上一个控制器或者某一个控制器:
- (IBAction)backToOne {
//回到根控制器
//[self.navigationController popToRootViewControllerAnimated:YES];
//跳转到第一个控制器,之前的控制器全部pop
[self.navigationController popToViewController:self.navigationController.viewControllers.firstObject animated:YES];
}
- (IBAction)backToTwo {
//移除栈顶控制器即可
[self.navigationController popViewControllerAnimated:YES];
}
二: UITabBarController 的简单介绍
和UINavigationController类似,UITabBarController 也可以轻松管理多个子控制器,轻松完成多个控制器之间的切换,典型的例子就是微信、QQ、微博等应用。

UITabBarController添加控制器的方式有两种,分别是:
(1):添加单个子控制器
- (void)addChildViewController:(UIViewController *)childController;
(2):设置子控制器数组
@property (nonatomic,copy) NSArray *viewControllers;
在UITabBarController 中,如果其有N个控制器,则UITabBar内就会有N个UITabBarButton 作为子控件。如果UITab有4个子控制器,那么UITabBar的结构大致如下图所示:

UITabBarButton里面显示什么内容,是由对应子控制器的tabBarItem属性决定。如下图:

UITabBarController 的一个示例程序:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds] ];
self.window.backgroundColor = [UIColor whiteColor];
//1.创建 tabbar控制器
UITabBarController *tabbarVC = [[UITabBarController alloc] init];
self.window.rootViewController = tabbarVC;
//2.添加子控制器
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor grayColor];
vc1.tabBarItem.title = @"联系人";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor"];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor lightGrayColor];
vc2.tabBarItem.title = @"动态";
vc2.tabBarItem.image = [UIImage imageNamed:@"tab_qworld_nor"];
vc2.tabBarItem.badgeValue = @"";
UIViewController *vc3 = [[UIViewController alloc] init];
vc3.view.backgroundColor = [UIColor lightTextColor];
vc3.tabBarItem.title = @"设置";
vc3.tabBarItem.image = [UIImage imageNamed:@"tab_me_nor"];
tabbarVC.viewControllers = @[vc1,vc2,vc3];
[self.window makeKeyAndVisible];
return YES;
}
效果图如下:

三:小结
在主流的App中,通常是将 UINavigationController 和 UITabBarController 结合使用。通常情况下,将UITabBarController做为根控制器,UITabBarController的子控制器是 UINavigationController,然后UINavigationController的子控制器又是一个个UIViewController。其结构示意图如下:

iOS中多控制器的使用的更多相关文章
- iOS中UINavigationController控制器使用详解
一.概述 UINavigationController用来管理视图控制器,在多视图控制器中常用.它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),push和pop方法来弹 ...
- IOS中导航控制器的代理及隐藏控制器刚出现时的滚动条
一.导航控制器的代理 1.UINavigationController的delegate属性 2.代理方法 1> 即将显示新控制器时调用 /* navigationController : 导航 ...
- iOS中分段控制器与UIScrollView结合使用
指定根视图: // 设置window的根视图控制器 self.window.rootViewController = [[UINavigationController alloc] initWithR ...
- iOS:iOS中的多控制器管理
iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@" ...
- IOS中的UINavigationController(导航控制器)
UINavigationController UINavigationControlle:导航控制器,是iOS中最常用的多视图控制器之一,它用来管理多个试图控制器 导航控制器可以认为是管理控制器的控制 ...
- Swift - iOS中各种视图控制器(View Controller)的介绍
在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...
- NSTimer 销毁问题 和 iOS中控制器的释放问题
俗话说的好,前人栽树后人乘凉,最近看了很多博文,不少博文提到了NSTimer的销毁问题, 之前我都没怎么注意,现在对照着文章一一实践发现坑还真不少.下面是我读到的几篇博文分享给大家 @啸笑天的NSTi ...
- iOS中控制器的释放问题
iOS中控制器的释放问题 ARC工程是可以重写dealloc方法并被系统调用的,但不需要手动调用父类的dealloc,手写[super dealloc]方法会报错,事实上系统会自动帮你调用父类的dea ...
- iOS开发中视图控制器ViewControllers之间的数据传递
iOS开发中视图控制器ViewControllers之间的数据传递 这里我们用一个demo来说明ios是如何在视图控制器之间传递重要的参数的.本文先从手写UI来讨论,在下一篇文章中讨论在storybo ...
随机推荐
- websocket webworker
对我来说最快的学习途径是实践,所以找两个东西来练手.一个是websocket一个是webwoker,今天先说第一个. 要理解socket就要先理解http和tcp的区别,简单说就是一个是短链,一个是长 ...
- hdu 4602 Partition(快速幂)
推公式+快速幂 公式有很多形式,可以写矩阵 1.前n-1项和的两倍+2的(n-2)次方,这个写不出啥 2.递推式:f(n)=2*f(n-1)+2的(n-3)次方 3.公式:2的(n-k-2)次方*(n ...
- Java [Leetcode 319]Bulb Switcher
题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...
- C++实现String
# include <iostream> # include <memory> # include <cstring> using namespace std; c ...
- Python中文乱码的处理
为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x96\x87”的形式? 为什么会报错“UnicodeEncodeError: 'asc ...
- 【转】u盘不显示盘符
转自http://jingyan.baidu.com/article/f3ad7d0fd0793e09c3345b31.html 我的情况: 电脑只有一个c盘,插入u盘,u盘的盘符为d. 弹出u盘,但 ...
- Android WindowManager 监听返回键及home键
一.监听home键盘,Android Home键系统负责监听,捕获后系统自动处理.有时候,我们需要监听home键处理自己的逻辑,监听方法如下: /** * 监听home键广播 */ private f ...
- uC/OS-II 移植笔记
用过51.AVR.Freescale.STM32,但是写程序一直没有用过实时操作系统,一是因为写的项目不大,二是不太想去看手册学东西.现在写的项目也算比较大,因为需要,所以就学一下,这样也不至于每次的 ...
- 利用Spring.Net技术打造可切换的分布式缓存读写类
利用Spring.Net技术打造可切换的Memcached分布式缓存读写类 Memcached是一个高性能的分布式内存对象缓存系统,因为工作在内存,读写速率比数据库高的不是一般的多,和Radis一样具 ...
- CentOS7 mariadb 修改编码
CentOS7 mariadb 编码的修改: 网上看了不少的解决方案,要么是比较老的,要么是不正确,测试成功的方式,记录备查. 登录MySQL,使用SHOW VARIABLES LIKE 'chara ...