整理之前寫的文章
摘要
在項目開發(fā)過程中,點擊一個cell,進入到下一個vc,界面是一樣的,由于本身的VC是push進來的,自控制器不想使用push,就想到了addChildViewController。
內(nèi)容
主要創(chuàng)建一個數(shù)組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!)
}
}
}
之前文章