1.如何创建一个控制器
控制器常见的创建方式有以下几种
1.1通过storyboard
创建
//先加载storyboard文件(Test是storyboard的文件名)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
//接着初始化storyboard中的控制器
//初始化“初始控制器”(箭头所指的控制器)
ViewController *vc = [storyboard instantiateInitialViewController];
//通过一个标识初始化对应的控制器
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"identifier"];
1.2直接创建 ViewController *vc = [[ViewController alloc] init];
1.3指定xib文件
来创建
ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController"
bundle:nil];
2.控制器的view

控制器view的延迟加载
2.1控制器的view是延迟加载的
:用到时再加载
2.2可以用isViewLoaded
方法判断一个UIViewController的view是否已经被加载
2.3控制器的view加载完毕就会调用viewDidLoad
方法
3.多控制器
一个iOS的app很少只有一个控制器组成,除非这个app极其简单
当app中有多个控制器的时候,我们就需要对这些控制器进行管理
有多个view时,可以用一个大的view去管理1个或者多个小view
控制器也是如此,用1个控制器去管理其他多个控制器
比如,用一个控制器A去管理3个控制器B、C、D
控制器A被称为控制器B、C、D的“父控制器”
控制器B、C、D的被称为控制器A的“子控制器”
为了便于管理控制器,iOS提供了2个比较特殊的控制器
UINavigationController
UITabBarController
3.1UINavigationController
利用UINavigationController
,可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是系统自带的“设置”应用



3.2UINavigationController的使用步骤
1>初始化UINavigationController
2>设置UIWindow的rootViewController为UINavigationController
3>根据具体情况,通过push方法添加对应个数的子控制器
UINavigationController以栈的形式保存子控制器
@property(nonatomic,copy) NSArray *viewControllers;
@property(nonatomic,readonly) NSArray *childViewControllers;
//使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController
animated:(BOOL)animated;
//使用pop方法可以移除控制器
//将栈顶的控制器移除
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
//回到指定的子控制器
- (NSArray *)popToViewController:(UIViewController *)viewController
animated:(BOOL)animated;
//回到根控制器(栈底控制器)
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
3.3修改导航栏的内容
导航栏的内容由栈顶控制器的navigationItem属性决定
UINavigationItem有以下属性影响着导航栏的内容
左上角的返回按钮
@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;
中间的标题视图
@property(nonatomic,retain) UIView *titleView;
中间的标题文字
@property(nonatomic,copy) NSString *title;
左上角的视图
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;
右上角的视图
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;
4.Segue
Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue
对象(简称Segue)

每一个Segue对象,都有3个属性
唯一标识
@property (nonatomic, readonly) NSString *identifier;
来源控制器
@property (nonatomic, readonly) id sourceViewController;
目标控制器
@property (nonatomic, readonly) id destinationViewController;

4.1Segue的类型
根据Segue的执行(跳转)时刻,Segue可以分为2大类型
1>自动型
:点击某个控件后(比如按钮),自动执行Segue,自动完成界面跳转
2>手动型
:需要通过写代码手动执行Segue,才能完成界面跳转
4.1.1自动型Segue
按住Control键
,直接从控件拖线
到目标控制器

点击“登录”按钮后,就会自动跳转
到右边的控制器
如果点击某个控件后,不需要做任何判断,一定要跳转到下一个界面,建议使用“自动型Segue”
4.1.2手动型Segue
按住Control
键,从来源控制器
拖线到目标控制器

手动型的Segue需要设置一个标识
在恰当的时刻,使用perform方法执行对应的Segue
[self performSegueWithIdentifier:@"login2contacts" sender:nil];
// Segue必须由来源控制器来执行,也就是说,这个perform方法必须由来源控制器来调用
1>根据identifier去storyboard中找到对应的线,新建UIStoryboardSegue对象
设置Segue对象的sourceViewController(来源控制器)
新建并且设置Segue对象的destinationViewController(目标控制器)
2>调用sourceViewController的下面方法,做一些跳转前的准备工作并且传入创建好的Segue对象
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
// 这个sender是当初performSegueWithIdentifier:sender:中传入的sender
3>调用Segue对象的
- (void)perform;方法开始执行界面跳转操作
如果segue的style是push
取得sourceViewController所在的UINavigationController
调用UINavigationController的push方法将destinationViewController压入栈中,完成跳转
如果segue的style是modal
调用sourceViewController的presentViewController方法将destinationViewController展示出来

如果点击某个控件后,需要做一些判断,也就是说:满足一定条件后才跳转到下一个界面,建议使用“手动型Segue”
5.控制器的数据传递
控制器之间的数据传递主要有2种情况:顺传和逆传
1>顺传
控制器的跳转方向: A --> C 数据的传递方向 A --> C
数据的传递方式 : 在A的prepareForSegue:sender:
方法中根据segue参数取得destinationViewController, 也就是控制器C, 直接给控制器C传递数据,要在C的viewDidLoad
方法中取得数据,来赋值给界面上的UI控件

2>逆传
控制器的跳转方向: A --> C 数据的传递方向 C --> A
数据的传递方式 : 让A成为C的代理, 在C中调用A的代理方法,通过代理方法的参数传递数据给A

6.UITabBarController
跟UINavigationController
类似,UITabBarController
也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是QQ、微信等应用



UITabBarController的使用步骤
1>初始化UITabBarController
2>设置UIWindow
的rootViewController
为UITabBarController
3>根据具体情况,通过addChildViewController
方法添加对应个数的子控制器
UITabBarController的子控制器
UITabBarController添加控制器的方式有2种
添加单个子控制器
- (void)addChildViewController:(UIViewController *)childController;
设置子控制器数组
@property(nonatomic,copy) NSArray *viewControllers;
6.1UITabBar
如果UITabBarController有N个子控制器
,那么UITabBar
内部就会有N个UITabBarButton
作为子控件
如果UITabBarController有4个子控制器,UITabBar的结构

6.1.1UITabBarButton
UITabBarButton里面显示什么内容,由对应子控制器的tabBarItem
属性决定
UITabBarItem有以下属性影响着UITabBarButton的内容
标题文字
@property(nonatomic,copy) NSString *title;
图标
@property(nonatomic,retain) UIImage *image;
选中时的图标
@property(nonatomic,retain) UIImage *selectedImage;
提醒数字
@property(nonatomic,copy) NSString *badgeValue;


7.Modal
除了push
之外,还有另外一种控制器的切换方式,那就是Modal
任何控制器都能通过Modal的形式展示出来
Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止
//以Modal的形式展示控制器
- (void)presentViewController:(UIViewController *)viewControllerToPresent
animated:(BOOL)flag
completion:(void (^)(void))completion
//关闭当初Modal出来的控制器
- (void)dismissViewControllerAnimated:(BOOL)flag
completion:(void (^)(void))completion;
网友评论