您的位置:首页 > 移动开发 > IOS开发

iOS addChildViewController方法

2017-02-08 10:59 429 查看
单独使用 addSubview 去加viewController的view发现有许多问题,主要是使用了代理方法的。查了下发现问题所在。

苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。

对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。

//点击切换页面

- (void)didClick:(UIButton *)button

{

// 点击处于当前页面的按钮,直接跳出

其他页面

[self replaceController:self.currentVC newController:self.firstVC];

}

// 切换各个标签内容

- (void)replaceController:(UIViewController )oldController newController:(UIViewController )newController

{

/**

* 着重介绍一下它

* transitionFromViewController:toViewController:duration:options:animations:completion:

* fromViewController 当前显示在父视图控制器中的子视图控制器

* toViewController 将要显示的姿势图控制器

* duration 动画时间(这个属性,old friend 了 O(∩_∩)O)

* options 动画效果(渐变,从下往上等等,具体查看API)

* animations 转换过程中得动画

* completion 转换完成

*/

[self addChildViewController:newController];
[self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {

if (finished) {
//移除oldController,但在removeFromParentViewController:方法前
4000
不会调用willMoveToParentViewController:nil 方法,所以需要显示调用
[newController didMoveToParentViewController:self];
[oldController willMoveToParentViewController:nil];
[oldController removeFromParentViewController];
self.currentVC = newController;

}else{

self.currentVC = oldController;

}
}];


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: