-3

I have a UIViewController that has two containers and each Container is related to a UIViewController for some specific functionalities.

For people who are devaluating my question, it will be more helpful and appreciated if you put me on the right path instead.

what I am trying to do is pass data from the parent ViewController to the childViewControllers

  • I tried it using the protocol/delegate: But the problem is, I couldn't assign the delegate to the childViewContainer since it doesn't have an instance from the parent.

  • My second try was using the prepare function, but it doesn't work as well since the two containers load once the parent loads at first. so if the value is changed in the parentViewController I can't pass it again to the child.

enter image description here

Any Idea, please?

Elin
  • 105
  • 1
  • 7
  • 1
    Show the code where the parent creates these children. – trndjc Aug 16 '20 at 16:20
  • I created them with the storyboard and I am using their "alpha= 0 or alpha = 1" to switch between them – Elin Aug 16 '20 at 16:21
  • Have you checked into `Notifications`? (I still believe the delegate pattern should work better, but you really should show enough for me to duplicate you *exact* issue.) –  Aug 16 '20 at 23:08

1 Answers1

0

After a deeper digging I was able to find a solution for my own question. here I am going to post if anyone else needs it in the future

so, first of all, I need it to lunch the ChildContoller from the parent controller and not from the storyboard ( so I deleted the segue between the parent and the child. create a variable for childController like that:

 lazy  var firstChildViewController: FirstChildViewController =  {
        let storynoard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storynoard.instantiateViewController(identifier: "firstChild") as! FirstChildViewController
    self.addChild(viewController)
    self.view.addSubview(viewController.view)
        return viewController
        
    }()

same thing for the other one if you have two children

and then in the viewDidLoad:

   override func viewDidLoad() {
        
        super.viewDidLoad()
        firstChildViewController.view.isHidden  = false
        secondChildViewController.view.isHidden = true
    }

and then in the FirstChildViewController:

    override func viewDidLoad() {
    
    super.viewDidLoad()
    if let parent = self.parent as? ParentViewController {
     parent.delegate = self
 }

}

And the problem is solved Hope it helps someone

Elin
  • 105
  • 1
  • 7