在项目开发过程中,点击一个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!)
}
}
}