gpt4 book ai didi

ios - 将 ViewController 嵌入到一个主视图 Controller 的多个 ContainerView 中

转载 作者:行者123 更新时间:2023-11-30 11:50:00 24 4
gpt4 key购买 nike

我使用此代码(来自 this 答案)以及分段控件在容器 View 中的 viewController 之间切换,同时我使用它在第二个容器 View 中显示另一个 viewController相同的 View Controller 。因此,我必须注释掉删除先前 VC 的原始代码,以便嵌入两个容器 View 。 (1)如何清理之前的VC? (2) 请注意,我还将依赖项注入(inject)到子 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

}

最佳答案

这感觉有点像黑客,但这就是我在嵌入新的 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)
}
}

关于ios - 将 ViewController 嵌入到一个主视图 Controller 的多个 ContainerView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48435602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com