iOS学习之——Container View Controller

来源:互联网 发布:vmware安装ubuntu禁用 编辑:程序博客网 时间:2024/06/08 15:10

一、Container View Controller

一个iOS的app很少只由一个ViewController组成,除非这个app极其简单。 当有多个View Controller的时候,我们就需要对这些View Controller进行管理。 那些负责一个或者多个View Controller的展示并对其视图生命周期进行管理的对象,称之为容器,大部分容器本身也是一个View Controller,这样的容器可以称之为Container View Controller,Container View Controller 简称容器类View Controller,在UIKit中,提供诸如UINavgationController、UITabBarController、UIPageViewController和UISplitViewController等,但有时候我们需要自己定义我们App中具有独特交互逻辑的管理其他视图控制器的容器类View Controller,比方说侧边栏,比方说ViewPager似的管理类试图控制器。有个别容器不是View Controller,比如UIPopoverController其继承于NSObject。即容器型的viewController能够把app分为很多小而简单的部分来管理,允许他们一起无缝的展示用户界面。

二、API
addChildViewController:
removeFromParentViewController
transitionFromViewController:toViewController:duration:options:animations:completion:
willMoveToParentViewController:
didMoveToParentViewController:
基本上就是这几个方法。

三、用法
添加子节点

//定义child  First *first = [[First alloc]initWithNibName:@"First" bundle:nil];  //添加child  [self addChildViewController:first];  

获得子节点

[self.childViewControllers objectAtIndex:0];  

添加进入的子节点是有顺序的,从0开始。
获取子节点的个数

self.childViewControllers.count

子节点的切换
使用下面的方法:

- (void)transitionFromViewController:(UIViewController *)fromViewControllertoViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

参数:
fromViewController:必须是当前正在显示的子节点
toViewController:是当前没有被显示的子节点
duration:动画的时长,以秒为单位,如果设置为0则不显示动画。
Options:选择怎么样去表现动画,UIViewAnimationOptions是一个枚举,里面包含了很多表现动画的方式,例如UIViewAnimationOptionTransitionFlipFromBottom从底部翻滚上来。
Animations:在这里可以添加动画,用于改变view。
Completion:动画完成之后会调用的闭包,YES为动画完成,NO为动画被跳过了。

把child移除parent
removeFromParentViewController方法

[[self.childViewControllers objectAtIndex:0]removeFromParentViewController];//移出第一个child  

willMoveToParentViewController:与didMoveToParentViewController:
有点奇怪,我本以为这两个方法都应该是当child被加入或者移出parent的时候被调用,按照willMoveToParentViewController到didMoveToParentViewController的顺序。
但是测试结果有些奇怪,当child被加入到parent的时候只调用了willMoveToParentViewController,而当child被移出parent的时候只调用了didMoveToParentViewController方法,不解。看SDK。

willMoveToParentViewController:

Called just before the view controller is added or removed from a container viewcontroller.

也就是说被在添加或者移出前都会调用这个方法,继续看。

Your view controller can override this method when it needs to know that it has been added to a container. If you are implementing your own container view controller, it must call the willMoveToParentViewController: method of the child view controller before calling theremoveFromParentViewController method, passing in a parent value of nil.

意思是,当你实现自己的container viewcontroller的时候,在使用removeFromParentViewController方法移出child之前,首先要手动调用willMoveToParentViewController,并且设置parent参数为nil才行。所以说child移出的时候根本不会自动调用willMoveToParentViewController==。。。

When your custom container calls the addChildViewController: method, it automatically calls the willMoveToParentViewController: method of the view controller to be added as a child before adding it.

而当调用addChildViewController添加child的时候则会被自动调用。

再来看看didMoveToParentViewController。
didMoveToParentViewController:

Calledafter the view controller is added or removed from a container view controller.

当子节点已经被添加或者移出容器的时候被调用。

Discussion Your view controller can override this method when it wants to react tobeing added to a container. If you are implementing your own container view controller, it must call the didMoveToParentViewController: method of the child view controller after the transition to the newcontroller is complete or, if there is no transition, immediately after callingtheaddChildViewController: method.

必须在从一个child到另一个child的转换之后(transitionFromViewController方法),手动调用didMoveToParentViewController方法。如果没有转化则需要在addChildViewController之后立即调用。

The removeFromParentViewController method automatically calls
the didMoveToParentViewController: method of the child view controller after it removes thechild.

同理,didMoveToParentViewController也只会在子节点被移出时自动调用。

原文:

http://www.2cto.com/kf/201311/254997.html

Container View Controller详情请见:

http://www.jianshu.com/p/68a589e244e4

1 0
原创粉丝点击