[英]How to pass data to a embedded UIViewController in side a UIView - Swift 5
There's a view controller B. So, have embedded B as child view controller in viewController A in a UIView.有一个视图 controller B。因此,在 UIView 中的 viewController A 中嵌入了 B 作为子视图 controller。 Now, need to call a api in child view controller B when it is added as a child.
现在,需要在子视图 controller B 中调用 api 作为子视图。 So, need to set a check when added B as child only then this api need to be hit.
因此,需要在将 B 添加为子项时进行检查,然后才需要点击此 api。 Kindly help in this.
请在这方面提供帮助。
Below is the code, how i embedded B as child View in A viewController:下面是代码,我如何在 A viewController 中嵌入 B 作为子视图:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vcController: UIViewController = storyboard.instantiateViewController(withIdentifier: "BNameViewController") as! BNameViewController
//add as a childviewcontroller
addChild(vcController)
// Add the child's View as a subview
viewBList.addSubview(vcController.view)
vcController.view.frame = viewBList.bounds
vcController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// tell the childviewcontroller it's contained in it's parent
vcController.didMove(toParent: self)
viewBList is the UIView in View controller A. So, now need in this case an api to be called in VC B not when we navigate to B from any other VC. viewBList 是视图 controller A 中的 UIView。所以,在这种情况下,现在需要在 VC B 中调用 api,而不是当我们从任何其他 VC 导航到 B 时。 I tried passing a bool or string by declaring same in VC B and the pass value from A, that doesn't work.
我尝试通过在 VC B 中声明相同的值和来自 A 的传递值来传递布尔值或字符串,但这是行不通的。 Please guide.
请指导。 Hope i made my question clear, please feel free to ask if any doubt.
希望我把我的问题说清楚了,如果有任何疑问,请随时提出。
There are so many ways to do this, When you are navigating to view controller B from some other view controller then you have navigationController property not nil.有很多方法可以做到这一点,当您从其他视图 controller 导航查看 controller B 时,您的 navigationController 属性不为零。
In that case you can check like that在这种情况下,您可以像这样检查
override func viewDidLoad(animated: Bool) {
super.viewDidLoad(animated: animated)
//If navigating from some view controller then
if self.navigationController != nil {
//Code here for case, when you are navigating from some one
}
//View controller is child of someone
if (self.navigationController == nil || self.presentingViewController == nil || self.parent != nil ) {
//This is your case,
// hit api here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.