簡體   English   中英

如何將數據傳遞到 UIView 一側的嵌入式 UIViewController - Swift 5

[英]How to pass data to a embedded UIViewController in side a UIView - Swift 5

有一個視圖 controller B。因此,在 UIView 中的 viewController A 中嵌入了 B 作為子視圖 controller。 現在,需要在子視圖 controller B 中調用 api 作為子視圖。 因此,需要在將 B 添加為子項時進行檢查,然后才需要點擊此 api。 請在這方面提供幫助。

下面是代碼,我如何在 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 是視圖 controller A 中的 UIView。所以,在這種情況下,現在需要在 VC B 中調用 api,而不是當我們從任何其他 VC 導航到 B 時。 我嘗試通過在 VC B 中聲明相同的值和來自 A 的傳遞值來傳遞布爾值或字符串,但這是行不通的。 請指導。 希望我把我的問題說清楚了,如果有任何疑問,請隨時提出。

有很多方法可以做到這一點,當您從其他視圖 controller 導航查看 controller B 時,您的 navigationController 屬性不為零。

在這種情況下,您可以像這樣檢查

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM