[英]How to pass data from parent view controller to child container view controller
I am trying to send data from ParentVC to a childVC using a container.我正在尝试使用容器将数据从 ParentVC 发送到 childVC。 The container is not embedded as it is used multiple times in my app, so I cannot use prepareForSegue.
该容器未嵌入,因为它在我的应用程序中多次使用,因此我无法使用 prepareForSegue。 I am instantiating the containerVC in ViewDidLoad.
我正在 ViewDidLoad 中实例化 containerVC。 How do I pass data to the containerVC on load?
如何在加载时将数据传递给 containerVC? I have seen answers on SO to passing from Child to Parent, but not this way round.
我已经看到了关于从孩子传递到父母的答案,但不是这样。 Thanks in advance for any help.
在此先感谢您的帮助。
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?
}
}
You can try it more early like你可以早点试试
let childVC = storyboard!.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC
childVC.boolVariable = true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.