如何将数据从 parent 视图控制器传递到 child 容器视图控制器
How to pass data from parent view controller to child container view controller
我正在尝试使用容器将数据从 ParentVC 发送到孩子 VC。该容器未嵌入,因为它在我的应用程序中多次使用,因此我无法使用 prepareForSegue。我在 ViewDidLoad 中实例化容器VC。如何在加载时将数据传递给容器VC?我已经看到关于从 Child 传递到 Parent 的答案,但不是这样。在此先感谢您的帮助。
import UIKit
class ParentVC: UIViewController {
@IBOutlet var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let childVC = storyboard!.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC
addChild(childVC)
childVC.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
childVC.view.frame = containerView.bounds
containerView.addSubview(childVC.view)
childVC.didMove(toParent: self)
// pass values to child
childVC.boolVariable = true // THIS DOESN'T WORK. HOW TO PASS DATA TO THE CHILD?
}
}
你可以像
一样早点尝试
let childVC = storyboard!.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC
childVC.boolVariable = true
我正在尝试使用容器将数据从 ParentVC 发送到孩子 VC。该容器未嵌入,因为它在我的应用程序中多次使用,因此我无法使用 prepareForSegue。我在 ViewDidLoad 中实例化容器VC。如何在加载时将数据传递给容器VC?我已经看到关于从 Child 传递到 Parent 的答案,但不是这样。在此先感谢您的帮助。
import UIKit
class ParentVC: UIViewController {
@IBOutlet var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let childVC = storyboard!.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC
addChild(childVC)
childVC.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
childVC.view.frame = containerView.bounds
containerView.addSubview(childVC.view)
childVC.didMove(toParent: self)
// pass values to child
childVC.boolVariable = true // THIS DOESN'T WORK. HOW TO PASS DATA TO THE CHILD?
}
}
你可以像
一样早点尝试let childVC = storyboard!.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC
childVC.boolVariable = true