How to pass data from child to parent view controller? in swift

前端 未结 5 722
攒了一身酷
攒了一身酷 2020-12-16 02:05

I created an activity where when one of the text fields clicked it will pop up a child(alert dialog) with list of product but when i click one item on the list I can\'t disp

5条回答
  •  [愿得一人]
    2020-12-16 02:28

    In ChildVC(ViewPopUpProduct) add instance of ParentVC

    class ViewPopUpProduct: UIViewController {
      var parentVC = ViewAward? //ParentView
      var someValueToSend : String? 
      .
      .
      .
      fun sendData(){
        // After fetching some value call this function from child
        parentVC.result = someValueToSend
        self.view.removeFromSuperview()
      }
    }
    

    In Parent View While you invoke child(subview) share the instance

    class ViewAward: UIViewController{
    
       var result = String?//Variable to store result from child
    
       func productTapped(textfield: UITextField){
    
        //tfProduct.endEditing(true)
        tfProduct.resignFirstResponder()
    
        let popOverVC = UIStoryboard(name:"Main",bundle:nil).instantiateViewControllerWithIdentifier("sbPopUpID") as! ViewPopUpProduct
    
        popOverVC.parentVC = self//Sharing Parent Views Instance
    
        self.addChildViewController(popOverVC)
    
        popOverVC.view.frame = self.view.frame
    
        self.view.addSubview(popOverVC.view)
    
        popOverVC.didMoveToParentViewController(self)
     }
    }
    

    now after removing the child view Access the Result variable and enjoy!

提交回复
热议问题