1

I have a Main VC (call this VC A), that has a child VC (VC B). When I tap a button on VC B I dismiss it as a child VC, however once this is done I would like to instantiate another VC (VC C). I do this by creating a bool on VC B which, if true calls a function on VC A that creates a new child VC (VC C). All of the function calls are being made however the VC C never gets added. Below I have added the code:

VC B:

 func removeAnimate()
{

    self.willMove(toParentViewController: nil)
    self.view.removeFromSuperview()
    self.removeFromParentViewController()

    didTransition = true

    if didTransition == true {
       callAddVC()
    }

}

func callAddVC() {
    let instVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController") as! MainViewController

    instVC.addVC()
}

VC A:

  func addVC () {


    let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CommentsTabViewController") as! CommentsTabViewController

    self.addChildViewController(popvc)
    popvc.view.center = self.view.center

    popvc.view.bounds.size = CGSize(width: 337, height: 503)

    self.view.addSubview(popvc.view)

    popvc.didMove(toParentViewController: self)
            }
NightHawk95
  • 163
  • 3
  • 11

2 Answers2

2

You are creating new instance of ViewController A (MainViewController) on the callAddVC(), Which is wrong. You are not using existing instance of the ViewController A

You have to pass the Viewcontroller A instance while adding a Viewcontroller B

Viewcontroller A

  let viewControllerB = // Get the instance of UIViewControllerB from storyboard
  viewControllerB.vcA = self

Viewcontroller B

  class UIViewControllerB {
        weak var vcA: UIViewControllerA?

         func removeAnimate() {

              self.willMove(toParentViewController: nil)
              self.view.removeFromSuperview()
              self.removeFromParentViewController()

              didTransition = true

             if didTransition == true {
               vcA.addVC(). //You have to call addVC() by using the reference of the main view controller.
             }
           }

     }
Subramanian P
  • 4,365
  • 2
  • 21
  • 25
  • thank you so much for this answer! For clarity I thought when a new VC is instantiated it is referenced however this shows me that I am actually creating a copy? – NightHawk95 Jul 13 '17 at 15:04
  • Not a "copy", it's equivalent to to having to lines of code `let vcA = MyVC()` `let vcB = MyVC()`. It's 2 unrelated instances. The word "copy" implies one is derived from the other. – allenh Jul 13 '17 at 15:12
  • @NightHawk95 When a new VC is allocated then it is pointing to different memory location. You have to pass the reference of already allocated one. – Subramanian P Jul 13 '17 at 15:21
  • ok, thank you for this clarification! Is there somewhere I can find more literature on these mechanics? – NightHawk95 Jul 13 '17 at 15:23
  • @NightHawk95 `storyboard.instantiateViewController(_)` will allocation a new instance, it will not point to the existing one – Subramanian P Jul 13 '17 at 15:23
  • @NightHawk95 This is about memory management. https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/MemoryManagement.html – Subramanian P Jul 13 '17 at 15:24
  • @NightHawk95 You are welcome :) Please accept the answer, if it solves your problem. – Subramanian P Jul 13 '17 at 15:33
1

let instVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController") as! MainViewController

This line creates (instantiates) a NEW instance of your MainViewController, it is not your current, displayed MainViewController.

You need to maintain a reference to the first MainViewController (possibly by accessing the rootViewController on the main window. And add the new popover to that.

allenh
  • 6,582
  • 2
  • 24
  • 40