0

I am stuck on one issue, I have a following case

  1. I have a Parent VC

  2. It has 2 containers [childVC A, childVC B]

  3. API is calling on childVC (UIViewController) A

  4. As soon as childVC A receives response, half response should be populated on same childVC A and half response should be populated on its Parent VC [at same time, without using segue]

For this I have gone through the delegate and protocol methodology using this, they have used the segue, I don't want the segue.

Example perspective, I have gone through the example of Add To Cart methodology in which user will select the product from childVC and value will be added to the cart of parent VC

Kiran S Kulkarni
  • 215
  • 1
  • 2
  • 16

1 Answers1

1

Adding the api call inside the child makes you have to use the delegate or access the parent if it's the rootvc , instead make the call inside the parent and use this to access the child

if let child1 =  self.children.first as?  FirstVC { // suppose it's at index 0
     // send the data to child 
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • 1
    Yeh thanx, I have implemented this and won – Kiran S Kulkarni Dec 03 '18 at 11:30
  • Instead of hunting in the parent view controller's list of children each time, I prefer to implement `prepare(for:sender:)` and save pointers to the children when the view hierarchy is being set up. Then you know exactly what you have and don't have to worry about changes to the child view controller array. – Duncan C Dec 04 '18 at 01:33