ViewController添加子控制器 并且弹出

/**

 *  初始化子控制器

 */

- (void)setupChildVcs

{

    for (int i = 0; i<6; i++) {

        UIViewController *vc = [[UIViewController alloc] init];

        vc.view.backgroundColor = HMRandomColor;

        [self addChildViewController:vc];

    }

    

    // 默认选中第0个控制器

    [self switchChildVc:0];

}

/**

 *  切换子控制器

 *

 *  @param index 最新子控制器的索引

 */

- (void)switchChildVc:(int)index

{

    // 切换子控制器

    // 移除当前正在显示的子控制器

    [self.showingChildVc.view removeFromSuperview];

    

    // 显示index对应的子控制器

    UIViewController *newChildVc = self.childViewControllers[index];

    [self.view addSubview:newChildVc.view];

    self.showingChildVc = newChildVc;

}

原文地址:https://www.cnblogs.com/yyj900165/p/4865777.html