美文网首页
Swift ----viewController 中addChi

Swift ----viewController 中addChi

作者: 天空像天空一样蓝 | 来源:发表于2018-04-16 15:15 被阅读0次

整理之前写的文章

摘要

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

内容

主要创建一个数组childVCs保存自己add进来的vc,直接上代码

添加ViewController

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()
    }
}

移除ViewController

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!)
        }
    }
}

之前文章

相关文章

网友评论

      本文标题:Swift ----viewController 中addChi

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