添加子控制器

https://github.com/soberZhichao/AddChildVC

- (IBAction)leftClick:(id)sender
{
    [self didRemoveCurrentVC];
    
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    self.currentVC = firstVC;
    
    [self addChildVC];
    
}

- (IBAction)rightClick:(id)sender
{
    [self didRemoveCurrentVC];

    SecondViewController *secondVC = [[SecondViewController alloc] init];
    self.currentVC = secondVC;
    
    [self addChildVC];
}

-(void)addChildVC
{
    [self addChildViewController:self.currentVC];
    //addChildViewController 会调用 [child willMoveToParentViewController:self] 方法,但是不会调用 didMoveToParentViewController:方法,官方建议显示调用
    [self.currentVC didMoveToParentViewController:self];
    self.currentVC.view.frame = CGRectMake(0, 100, 400, 300);
    [self.view addSubview:self.currentVC.view];
}

- (void)didRemoveCurrentVC
{
    if (self.currentVC)
    {
        [self.currentVC.view removeFromSuperview];
        [self.currentVC removeFromParentViewController];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,509评论 2 45
  • js对象的三种继承方式转载 2017年06月23日 15:07:56 13300一,js中对象继承对象的js中有三...
    郭子祥阅读 445评论 0 0
  • 2011年7月19日下午18点12分,我总算看到了这届学生的中招切线情况了,盼望了好几天的事情总算尘埃落定了! 自...
    西瓜甜甜啦阅读 756评论 7 32
  • 你要特别努力,在人前才显得特别容易。 人到25岁每一天照镜子都会恐慌好久,难以相信这个苹果肌不够饱满,眼角有细纹的...
    阿心酱eve阅读 149评论 0 0