前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS addChildViewController方法

iOS addChildViewController方法

作者头像
傅_hc
发布2018-07-04 09:17:04
1.8K0
发布2018-07-04 09:17:04
举报
文章被收录于专栏:iOS开发随笔iOS开发随笔

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

图1-1

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

代码语言:javascript
复制
//在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方法了:

代码语言:javascript
复制
#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

代码语言:javascript
复制
 //在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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.08.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档