iOS Container View Controller小小的研究

本文探讨了iOS中Container View Controller的重要性和使用,包括addChildViewController:、removeFromParentViewController等API,以及如何进行子控制器的添加、切换和移除操作。通过示例解释了在自定义容器时的方法调用顺序及其注意事项。
摘要由CSDN通过智能技术生成

一、介绍

老规矩先看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的时候被调用,按照willMoveToParentViewControllerdidMoveToParentViewController的顺序。

但是测试结果有些奇怪,当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

本文遵循“署名-非商业用途-保持一致”创作公用协议


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值