iOS addChildViewController方法

作者: 傅hc | 来源:发表于2016-08-02 23:23 被阅读24717次

APP中经常有根据标签来切换页面的需求,如果切换的页面只是刷新一下数据也就罢了,但是如果每个标签切换页面的数据和内容、结构完全不同你会怎么样做?(例如:图1-1)

图1-1

个人觉得理想的做法就是每个标签展示的内容为一个View,这样切换既不会影响之前View还可以快速切回之前的View,而且符合高聚合、低耦合开发啊,这里就要隆重介绍一下addChildViewController方法:

//在ViewController 中添加其他UIViewController,currentVC是一个UIViewController变量,存储当前显示的viewcontroller
    FirstVC * first = [[FirstVC alloc] init];
    [self addChildViewController:first];
    //addChildViewController 会调用 [child willMoveToParentViewController:self] 方法,但是不会调用 didMoveToParentViewController:方法,官方建议显示调用
    [first didMoveToParentViewController:self];
    [first.view setFrame:CGRectMake(0, CGRectGetMaxY(myScrollView.frame), width, height-CGRectGetHeight(myScrollView.frame))];
    currentVC = first;
    [self.view addSubview:currentVC.view];
//这里没有其他addSubview:方法了,就只有一个,而且可以切换视图,是不是很神奇?
    second = [[SecondVC alloc] init];
    [second.view setFrame:CGRectMake(0,CGRectGetMaxY(myScrollView.frame), width, height-CGRectGetHeight(myScrollView.frame))];

苹果已经给我写好切换UIViewController的transitionFromViewController方法了:


#pragma mark - 切换viewController
- (void)changeControllerFromOldController:(UIViewController *)oldController toNewController:(UIViewController *)newController
{
    [self addChildViewController:newController];
    /**
     *  切换ViewController
     */
    [self transitionFromViewController:oldController toViewController:newController duration:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        //做一些动画
        
    } completion:^(BOOL finished) {
       
        if (finished) {
            
            //移除oldController,但在removeFromParentViewController:方法前不会调用willMoveToParentViewController:nil 方法,所以需要显示调用
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            currentVC = newController;
            
        }else
        {
            currentVC = oldController;
        }
        
    }];
}

效果如下:

图1-2 精选页面 图1-3 切换到发现页面

写到这里大家对addChildViewController有一定的了解了,当一个界面比较复杂的时候我们就可以采用这种方式来降低耦合度(如果各位有更加好的方法,希望不要吝惜交流一下),这样做对页面的逻辑更加分明,如果有可以重用的也方便重用,而且View没有显示也不会load,减少内存的使用。
同时,还可以在一个parent ViewController上添加多个child ViewController,实际中这样的页面也是挺多的,如图1-4

 //在ViewController 中添加其他UIViewController
    FirstVC * first = [[FirstVC alloc] init];
    [self addChildViewController:first];
    //addChildViewController 会调用 [child willMoveToParentViewController:self] 方法,但是不会调用 didMoveToParentViewController:方法,官方建议显示调用
    [first didMoveToParentViewController:self];
    [first.view setFrame:CGRectMake(0, CGRectGetMaxY(myScrollView.frame), width, 300)];
    [self.view addSubview:first.view];
    
    SecondVC * second = [[SecondVC alloc] init];
    [self addChildViewController:second];
    [second didMoveToParentViewController:self];
    [second.view setFrame:CGRectMake(0,CGRectGetMaxY(first.view.frame), width, 300)];
    [self.view addSubview:second.view];
图1-4

相关文章

网友评论

  • ea5f415864e2:请问我通过addChildViewController添加了navigationController进去了,navigationController里面跳转(push)的时候如何让新页面是全屏呢
  • 童话镇里蜿蜒的河:能用修改父控制器 导航栏的信息吗? 具体怎么操作
    石显军:self.parentViewController.title
    傅hc:能啊,你可以全局改或者通过属性持有父控制器修改
  • 时光管理员:求解,我添加完子控制器,状态栏的颜色改变了
    时光管理员:@傅hc 谢谢
    傅hc:@时光管理员 plist表也要设置一下
    时光管理员:使用UIStatusBarStyleLightContent改变也没有用
  • Happy_qaq:在切换viewController的时候是不是应该加一条判断条件啊?
    oldVC != newVC 才能够跳转,
    否则的话是会因为父视图不同而崩溃.
    傅hc:可以按需处理哈~~
  • 萨达搜索到:楼主你好,我也遇到了一个这样的问题,大概是15个页面,但是我多了一个滑动切换,我把vc的view加载到scrollview上了,苹果那种切换方式好像不太行啊
    傅hc:你这个需求可以看一下这个:http://www.jianshu.com/p/7f58936f3166
  • GavinKang:详解的真小白。。
  • CRAZYBADAM:楼主,addChildViewController里面加的子视图,子视图里面还可以addChildViewController 么
    傅hc:@CRAZYBADAM 可以,但是不建议这样做,你这样做不是更加复杂了吗?用这个方法就是为了解耦的:stuck_out_tongue_closed_eyes:
  • minirat:为什么childController中的按钮之类有动作的空间会不可用???是需要代理吗???
    小白求教。。。
    minirat:@傅hc 谢谢!!!谢谢!!!的确是这个问题,,,我想做一个侧栏,结果超出self.view,小白膜拜大神!!!
    傅hc:@minirat 是不是frame超过本身大小了?
    minirat:控件
  • 踏云小子:不错哦,很实用
  • d0ab8ef28fa2:用一个gif看起来就比较适合了
    傅hc:@omni_Hsin 哈哈,以后的都用gif哈
    omni_Hsin:@傅hc 就现在呗,别以后了
    傅hc:@鳗鱼饭_吃 以后会用gif,谢谢建议
  • 416703ce99a4:楼主,我看到有些人是这么写的[first didMoveToParentViewController:self]; //这种写法和你相反啊,,我不太懂这个意思!有什么区别啊,,
    傅hc:@FaiChou :smile: 谢谢提醒,已经改过来了
    FaiChou:@傅hc 改过来吧
    傅hc:@简单点ma 谢谢你指出,是我粗心写反了,正确是:[first didMoveToParentViewController:self],意思是将first添加到父容器类viewController上,所以应该是[first didMoveToParentViewController:self];
  • 小怡情ifelse::+1:楼主有demo么 可以传到github 方便大家下载哈
    傅hc:@厦大 核心代码已经在文章中了,其它都是没有什么用的代码,所以没有上传了
  • 清無:小白教程
    傅hc:@muzipiao :joy:

本文标题:iOS addChildViewController方法

本文链接:https://www.haomeiwen.com/subject/ppizjttx.html