How to transfer data between Parent and Child View Controllers

主宰稳场 提交于 2019-12-02 10:54:29

You can pass value from Parent to Child Controller like this

controller = (storyboard?.instantiateViewController(withIdentifier: "People"))
    addChildViewController(controller!)
    controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300)
    controller.tableDataSource = // Pass your required value to child controller
    self.view.addSubview((controller?.view)!)
    controller?.didMove(toParentViewController: self)

Now you want to transfer back your select value to Parent view controller. For this purpose your have a create a Delegate in ChildController like

@protocol ChildControllerDelegate : class {
    func selectedValue(Value : String)
}

After that make a variable of that delegate in ChildController like this

weak var delegate : ChildControllerDelegate?

and when in rowDidSelect method add following code

if(delegate != nil) {
   delegate.selectedValue(Value :"Your selected value")
}

Now step when you are going to show ChildController from ParentController at that time you have to set that delegate object to ParentController like this

controller = (storyboard?.instantiateViewController(withIdentifier: "People"))
    addChildViewController(controller!)
    controller?.view.frame = CGRect(x: 10, y: 200, width: 394, height: 300)
    controller.delegate = self
    self.view.addSubview((controller?.view)!)
    controller?.didMove(toParentViewController: self)

and after that just implement the delegate method in ParentController like that

func selectedValue(Value : String) {
   // you select val 
}

You can:

  • Cast the controller to the appropriate class for the child view controller (I'm using ChildViewController below, but hopefully you have a more descriptive name); and

  • Pass the array (which I guessed you might have called people, but use whatever your array names are in these two respective view controllers) from the current view controller (the parent) to this new child view controller.

Thus:

let child = storyboard!.instantiateViewController(withIdentifier: "People") as! ChildViewController
addChildViewController(child)
child.people = people
child.view.frame = ...
view.addSubview(child.view)
child.didMove(toParentViewController: self)

Personally, I wouldn't hard code the child coordinates like you did in your original question. I'd set translatesAutoresizingMaskIntoConstraints to false and then add the appropriate leading/trailing/top/bottom constraints, but that's up to you. It was just too painful to see hardcoded coordinates in your example.

Try This.

First Create Public Method For Add And Remove childVC.

For Add childVC.

public class func openChildViewController(parentVC:UIViewController, with childVC:UIViewController){

        parentVC.addChildViewController(childVC)
        childVC.view.frame = parentVC.view.frame
        parentVC.view.addSubview(childVC.view)
        parentVC.didMove(toParentViewController: childVC)
    }

For Remove childVC.

   public class func removeChildViewController(childVC:UIViewController){

        childVC.willMove(toParentViewController: nil)
        childVC.view.removeFromSuperview()
        childVC.removeFromParentViewController()
    }

Use Above Method.

1.ParentVC.swift

class ParentVC: UIViewController , ChildVCDelegate  {

     var arrType = NSMutableArray()

    //add ChildVC
    @IBAction func btnAddChildVC(_ sender: UIButton) {
        let ChildVC = self.storyboard?.instantiateViewController(withIdentifier: "ChildVC") as! ChildVC
        PickerVC.arrPass = arrType //for data passing create any object in ChildVC for ex. arrPass is NSMutableArray
        ChildVC.delegate = self
        openChildViewController(parentVC: self, with: ChildVC)
    }

     // MARK: ChildVC Delegate
   func SetSelectedPickerValue(strSelectOption: String) {
                print(strSelectOption)
        }
    }

}

2.ChildVC.swift

class ChildVC: UIViewController{

    // MARK: Variable for ParentVCData Passing
     var arrPass = NSMutableArray()

    override func viewWillAppear(_ animated: Bool) {
        print(arrPass)
    }

    //Remove ChildVC
    @IBAction func btnRemoveChildVC(_ sender: UIButton) {
        self.delegate?.SetSelectedPickerValue!(strSelectOption: “any String you pass ChildVC To ParentVC”)
        removeChildViewController(childVC: self)
    }
}
// MARK: Create Delegate Method
@objc protocol ChildVCDelegate{
     @objc optional func SetSelectedPickerValue(strSelectOption:String)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!