Objective-C学习之Container View Controller

本文介绍了Container View Controller在iOS开发中的重要性,它用于管理多个ViewController的展示。文中详细讨论了Container View Controller的主要API,包括添加、移除子节点的方法,并解析了在实现自定义容器时willMoveToParentViewController:和didMoveToParentViewController:方法的调用逻辑。文章还提供了相关链接供深入学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Container View Controller

       一个iOS的app很少只由一个ViewController组成,除非这个app极其简单。 当有多个View Controller的时候,我们就需要对这些View Controller进行管理。 那些负责一个或者多个View Controller的展示并对其视图生命周期进行管理的对象,称之为容器,大部分容器本身也是一个View Controller,这样的容器可以称之为Container View ControllerContainer View Controller 简称容器类View Controller,在UIKit中,提供诸如UINavgationControllerUITabBarControllerUIPageViewControllerUISplitViewController等,但有时候我们需要自己定义我们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:/td> 必须是当前正在显示的子节点
toViewController:是当前没有被显示的子节点
duration:动画的时长,以秒为单位,如果设置为0则不显示动画
Options:选择怎么样去表现动画,UIViewAnimationOptions是一个枚举,里面包含了很多表现动画的方式,例如UIViewAnimationOptionTransitionFlipFromBottom从底部翻滚上来
Animations:在这里可以添加动画,用于改变view
Completion:动画完成之后会调用的闭包,YES为动画完成,NO为动画被跳过了

       把child移除parent
       removeFromParentViewController方法

[[self.childViewControllers objectAtIndex: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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值