viewController中addChildViewContr

2017-10-26  本文已影响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!)

}

}

}

上一篇 下一篇

猜你喜欢

热点阅读