0

Added a child view controller as below:

// ParentViewController
 if ((self.delegate?.showPopupSymtomDetailViewController(view: viewController, receiver: self)) != nil)
    {

    }

// ChildViewController
 //MARK: PopUpCode
func popUpAt(parentViewController:UIViewController)
{
    parentViewController.addChildViewController(self)
    parentViewController.view.addSubview(self.view)

    self.view.frame = parentViewController.view.frame
    self.didMove(toParentViewController: parentViewController)

}

How to send data back to parentViewController, please guide.

iPhone 7
  • 1,731
  • 1
  • 27
  • 63

1 Answers1

0

You could use callbacks for instance.

class ParentViewController: UIViewController {

  if ((self.delegate?.showPopupSymtomDetailViewController(view: viewController, receiver: self)) != nil) {
     viewController = { YourString in 
          //do here whatever you want in your parent view controller
        }
  }
}

class ChildViewController: UIViewController {
   var callbackFunc: ((String) -> Void)? //You can decide if the object you want to send back, I used String but you can change it and even you can send more than one parameters back (String, ThirViewController, Bool)...

   func popUpAt(parentViewController:UIViewController) {
          parentViewController.addChildViewController(self)
          parentViewController.view.addSubview(self.view)

          self.view.frame = parentViewController.view.frame
          self.didMove(toParentViewController: parentViewController)
          callBack(YourString) //Here you pass the data from ChildViewController to ParentViewController
   }
}

There are other ways to send information back from ChildViewController to ParentViewController but that depends of your project. It's an action that you are going to do frequently? Maybe you need a delegate to reuse it.

Fran Martin
  • 2,369
  • 22
  • 19