iOS Container View Controller小小的研究
Posted 小杨在玩iOS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS Container View Controller小小的研究相关的知识,希望对你有一定的参考价值。
一、介绍
老规矩先看SDK
Container view controllers are a critical part of ios app design. They allow you todecompose your app into smaller and simpler parts, each controlled by a viewcontroller dedicated to that task. Containers allow these view controllers towork together to present a seamless interface.
就是说,容器型的viewController能够把app分为很多小而简单的部分来管理,允许他们一起无缝的展示用户界面。
其实应该已经接触过了一些container,viewcontrollers 经常很container一起工作。例如NavigationController、TabBar就是几个典型的container,它可以管理很多view controller。这次就来小小的研究一下customer container。
二、API
addChildViewController:
removeFromParentViewController
transitionFromViewController:toViewController:duration:options:animations:completion:
willMoveToParentViewController:
didMoveToParentViewController:
基本上就是这几个方法。
三、用法
- 添加子节点
//定义child
First *first = [[Firstalloc]initWithNibName:@"First"bundle:nil];
//添加child
[selfaddChildViewController:first];
- 获得子节点
[self.childViewControllersobjectAtIndex: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.childViewControllersobjectAtIndex: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 thatit has been added to a container.
If you are implementing your own container view controller, it must callthewillMoveToParentViewController: method of the child view controller before calling theremoveFromParentViewController method, passing in a parent value ofnil.
意思是,当你实现自己的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 callthedidMoveToParentViewController: 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 thedidMoveToParentViewController: method of the child view controller after it removes thechild.
同理,didMoveToParentViewController也只会在子节点被移出时自动调用。
杨光(atany)原创,转载请注明博主与博文链接,未经博主允许,禁止任何商业用途。
博文地址:http://blog.csdn.net/yang8456211/article/details/13996947
博客地址:http://blog.csdn.net/yang8456211
—— by atany
本文遵循“署名-非商业用途-保持一致”创作公用协议
以上是关于iOS Container View Controller小小的研究的主要内容,如果未能解决你的问题,请参考以下文章
IOS SDK: Container view with 3 UI View area如何?
《iOS Human Interface Guidelines》——Container View Controller
《iOS Human Interface Guidelines》——Container View Controller