addChildViewController相关
2015-07-08 21:27
405 查看
addChildViewController
相关
添加子控制器:-
(container.view addSubView:childController.view)
1.1
[container addChildViewController:childController]
1.2
[childController didMoveToParentViewController:container]
容器控制器添加子控制器,子控制器的
(void)willMoveToParentViewController:(UIViewController *)parent方法会自动被调用,但需要告诉系统何时添加完毕,必须手动调用子控制器的
didMoveToParentViiewController:
删除子控制器:
2.1
[childController.view removeFromSuperview]
2.2
[childController willMoveToParentViewController:nil]
2.3
[childController removeFromParentViewController]
先将自控器的视图从容器控制器视图中移除,在删除自控制器 之前 需要先调用子控制器的
willMoveToParentViewController:方法传入nil参数,之后才调用子控制器的
removeFromParentViewController方法,remove方法将自动调用
willMoveToParentViewController方法,无需手动调用。
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
这个方法比较有迷惑性,需要注意查看上面的注释说明,这个方法其实是用来管理view hierarchy,并不会影响 view controller hierarchy,所以这里你仍然需要自己管理view controler hierarchy。这个方法同时也比较有用,比如在切换child view controller的显示时,这个方法更加好用,可以参考官方guide中的代码实现:
- (void) cycleFromViewController: (UIViewController*) oldC toViewController: (UIViewController*) newC { [oldC willMoveToParentViewController:nil]; // 1 [self addChildViewController:newC]; newC.view.frame = [self newViewStartFrame]; // 2 CGRect endFrame = [self oldViewEndFrame]; [self transitionFromViewController: oldC toViewController: newC // 3 duration: 0.25 options:0 animations:^{ newC.view.frame = oldC.view.frame; // 4 oldC.view.frame = endFrame; } completion:^(BOOL finished) { [oldC removeFromParentViewController]; // 5 [newC didMoveToParentViewController:self]; }]; }
相关文章推荐
- LeetCode-Reverse Integer-解题报告
- C语言 switch语句的使用总结
- mysql循环方法总结
- poj 1811
- jQuery插件flexigrid使用总结
- Power of Two
- 判断回文数字
- 方法调用的先后顺序问题
- LeetCode-String to Integer (atoi) -解题报告
- Unity中NullReferenceException: (null) UnityEditor.SerializedObject..ctor (UnityEngine.Object[] 问题
- linux程序设计——进程管道(第十三章)
- Android Intent传递对象摘要
- 相位对焦与反差对焦
- Oracle EM错误,java.lang.Exception: Exception in sending Request :: null
- 查找(三)哈希表查找
- hdu 4609
- 《opencv入门》
- System.Threading.Timer 定制Web服务器定时器执行事务!
- c#第四次作业 c#操作MySQL数据库
- [leedcode 27] Remove Element