简体   繁体   English

如何将数据传递到 UIView 一侧的嵌入式 UIViewController - Swift 5

[英]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.

相关问题 如何在 Swift 中的 UIViewController 和 NSObject 之间传递数据 - How to pass data between a UIViewController and an NSObject in Swift 如何在Swift中将数据从UITableViewRowAction传递到UIViewController? - How to pass data to a UIViewController from a UITableViewRowAction in Swift? 如何将数字或数据从子类 UIView 传递到 UIViewController? - How to pass digit or data from subclassed UIView to UIViewController? 如何将数据从 UIViewsController 传递到 UIView,Swift - How to pass data from UIViewsController to UIView, Swift 如何重新加载UIVIew的UIViewController数据 - How to Reload UIViewController data of UIVIew 如何将数据从UIViewController传递到嵌入在该UIViewController上的容器中的UITableViewController? - How can I pass a data from UIViewController to a UITableViewController that is embedded in a container placed on that UIViewController? 如何将UIViewController作为参数传递给swift 3? - How to pass UIViewController as a parameter swift 3? 在Swift中为UIViewController定制UIView - Custom UIView for an UIViewController in Swift Swift-如何将数据从UITableViewCell类传递到UIViewController - Swift - How to Pass Data From UITableViewCell Class to UIViewController 重新加载UIView \\ UIViewController的数据? - Reload data for UIView\UIViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM