我正在使用此代码(来自this回答)以及分段控件在容器视图中切换viewControllers,同时我用它在第二个容器视图中显示另一个viewController在同一个viewController里面。因此,我必须注释掉删除前一个VC的原始代码,以便嵌入两个容器视图。 (1)我怎样才能清理以前的VC? (2)注意我也注入了儿童VC的依赖关系,这样做可以吗?
class ViewEmbedder {
class func embed(parent:UIViewController,date: Date, container:UIView, child:UIViewController, previous:UIViewController?){
// if let previous = previous {
// print("previous = \(previous.description)")
//// if previous != ScoreAndStatsTableViewController {
//// //remove
//// }
//
// removeFromParent(vc: previous)
// }
child.willMove(toParentViewController: parent)
if let hdtvc = child as? HealthDataTableViewController {
hdtvc.startDate = date
}
if let idtvc = child as? IceDataTableViewController {
idtvc.startDate = date
}
if let stvc = child as? ShiftTableViewController {
stvc.startDate = date
}
parent.addChildViewController(child)
container.addSubview(child.view)
child.didMove(toParentViewController: parent)
let w = container.frame.size.width;
let h = container.frame.size.height;
child.view.frame = CGRect(x: 0, y: 0, width: w, height: h)
}
class func removeFromParent(vc:UIViewController){
vc.willMove(toParentViewController: nil)
vc.view.removeFromSuperview()
vc.removeFromParentViewController()
}
class func embed(withIdentifier id:String, startDate: Date, parent:UIViewController, container:UIView, completion:((UIViewController)->Void)? = nil){
let vc = parent.storyboard!.instantiateViewController(withIdentifier: id)
embed(
parent: parent,
date: startDate,
container: container,
child: vc,
previous: parent.childViewControllers.first
)
completion?(vc)
}
}
用法:
class KingViewController: UIViewController {
var startDate = Date()
@IBOutlet weak var topContainerView: UIView!
@IBOutlet weak var bottomContainerView: UIView!
@IBAction func controlDidChange(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
ViewEmbedder.embed(withIdentifier: "IceDataTableViewController", startDate: startDate, parent: self, container: bottomContainerView) { (bvc) in
}
case 1:
ViewEmbedder.embed(withIdentifier: "HealthDataTableViewController",startDate: startDate, parent: self, container: bottomContainerView) { (bvc) in
}
case 2:
ViewEmbedder.embed(withIdentifier: "ShiftTableViewController", startDate: startDate, parent: self, container: bottomContainerView) { (stvc) in
}
答案 0 :(得分:0)
感觉有点像黑客,但这是我想到的,在嵌入一个新的VC之前清理现有的VC:仍然对任何更有说服力的建议持开放态度。
import Foundation
import UIKit
class ViewEmbedder {
class func embed(id: String, parent:UIViewController,date: Date, container:UIView, child:UIViewController, previous:UIViewController?){
let childVCArray = parent.childViewControllers
for vc in childVCArray {
print(vc.description)
let vcID = vc.restorationIdentifier
if vcID != id {
vc.removeFromParentViewController()
}
}
child.willMove(toParentViewController: parent)
if let hdtvc = child as? HealthDataTableViewController {
hdtvc.startDate = date
}
if let idtvc = child as? IceDataTableViewController {
idtvc.startDate = date
}
if let stvc = child as? ShiftTableViewController {
stvc.startDate = date
}
parent.addChildViewController(child)
container.addSubview(child.view)
child.didMove(toParentViewController: parent)
let w = container.frame.size.width;
let h = container.frame.size.height;
child.view.frame = CGRect(x: 0, y: 0, width: w, height: h)
}
class func removeFromParent(vc:UIViewController){
vc.willMove(toParentViewController: nil)
vc.view.removeFromSuperview()
vc.removeFromParentViewController()
}
class func embed(withIdentifier id:String, startDate: Date, parent:UIViewController, container:UIView, completion:((UIViewController)->Void)? = nil){
let vc = parent.storyboard!.instantiateViewController(withIdentifier: id)
embed(
id: id,
parent: parent,
date: startDate,
container: container,
child: vc,
previous: parent.childViewControllers.first
)
completion?(vc)
}
}