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 ...
随机推荐
- UVa 1149 Bin Packing 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...
- [转]ASP.NET数据库连接字符串总结
这个不难,而且很重要,但总忘,找了篇比较全的,作为资料. 原文链接http://developer.51cto.com/art/201107/275406.htm 关于数据库链接字符串的相关知识,我们 ...
- 【转】JAVA之动态代理
转自:像少年啦飞驰 代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 代理模式的结构如下图所示. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示 ...
- ffmpeg windows 雪花声解决方法
替换所有文件里的<math.h>为<mathimf.h>即可. 我用ffmpeg-0.6.3版测试时,好像mathimf.h文件和其他文件有冲突,需要修改源码. 和qdm2.c ...
- poj 3694 Network
题意: 添加每条新连接后网络中桥的数目// 超时 先放着了 ,下次改//早上这代码超时了 下午改了,代码在下面#include <iostream> #include <algori ...
- poj 1780 Code
//题目描述:KEY公司开发出一种新的保险箱.要打开保险箱,不需要钥匙,但需要输入一个正确的.由n位数字组成的编码.这种保险箱有几种类型,从给小孩子玩的玩具(2位数字编码)到军用型的保险箱(6位数字编 ...
- 【大数处理、正则表达式】NYOJ-513
[正则] 正则表达式是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符”). 模式描述在搜索文本时要匹配的一个或多个字符串. 常用字符: //正则表达式 //$ 匹配 ...
- [Papers]NSE, $\p_3u$, Lebesgue space [Kukavica-Ziane, JMP, 2007]
$$\bex \p_3\bbu\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad \frac{9}{4}\leq q\leq ...
- bjfu1099 度度熊大战僵尸
这也是2011年百度之星的一道题. 这题我就是乱搞搞过的,打代码之前自己心里也没底,不知道能不能过的. 我的做法很简单,就是按时间顺序依次构造能杀死的僵尸血量,找到第k小的.构造的方法也很暴力:对t时 ...
- 使用 gradle 编译多版本 android 应用
最近要做一个 android 产品的变种版本,需要编出不同版本,每个版本有不同的包名.图标等等,和一些特有的逻辑. 很久之前做过类似的工作,当时没有 gradle, 用的方法是把公共代码抽成一个 li ...