美文网首页
viewController中addChildViewContr

viewController中addChildViewContr

作者: 天空像天空一样蓝 | 来源:发表于2017-10-26 11:29 被阅读0次

    在项目开发过程中,点击一个cell,进入到下一个vc,界面是一样的,由于本身的VC是push进来的,自控制器不想使用push,就想到了addChildViewController。

    创建一个数组childVCs保存自己add进来的vc

    1.调用addChildViewcontroller()方法


    <code>fileprivate func addChildViewcontroller(){

    if self.chidlVCS.count > 0 {

    let lastVC = self.chidlVCS.last

    if lastVC != nil{

    lastVC?.view.removeFromSuperview()

    lastVC?.removeFromParentViewController()

    }

    }

    let VC = ViewController.init()

    self.view.addSubview(VC.view)

    self.addChildViewController(VC)

    self.chidlVCS.append(VC)

    weak var weakself = self

    VC.block = {

    weakself?.addChildViewcontroller()

    }

    VC.backBlock = {

    weakself?.removeChildViewcontroller()

    }

    }

    </code>

    fileprivate func removeChildViewcontroller(){

    if self.chidlVCS.count > 0 {

    let lastVC = self.chidlVCS.last

    if lastVC != nil{

    lastVC?.view.removeFromSuperview()

    lastVC?.removeFromParentViewController()

    self.chidlVCS.removeLast()

    }

    }

    if self.chidlVCS.count > 0 {

    let lastVC = self.chidlVCS.last

    if lastVC != nil{

    self.view.addSubview(lastVC!.view)

    self.addChildViewController(lastVC!)

    }

    }

    }

    相关文章

      网友评论

          本文标题:viewController中addChildViewContr

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