Search code examples
iosswiftuipageviewcontrollerunwind-segue

UIPageViewController stacking ViewControllers when segueing to home page


I have two storyboards, a main then a second storyboard which houses a UIPageViewController. Inside the second storyboard, an exit button allows a user to return to the home screen which is on the main storyboard.

When I attempt to segue back to the home screen, the page controller views are stacking. I have tried to use:

self.navigationController?.popViewController

Along with some other methods to deallocate the UIPageViewController sets. Nothing seems to work? How do I fix this?

You can see in the image below where the controllers are marked by (3).

Memory Graph Image

Below are a few of my attempts. I am using a notification to invoke a method on the root controller after the user confirms via a yes/no custom modal.

func attempt1() {
    self.viewControllerList.forEach {
        index in
        index.removeFromParentViewController()
        index.dismiss(animated: false, completion: nil)
        index.navigationController?.popViewController(animated: false)
    }
   self.performSegue(withIdentifier: "unwindHome", sender: self)
}

func attempt2() {
    self.childViewControllers.forEach {
        c in
        print("CHILD...>", c)
        c.removeFromParentViewController()
        c.dismiss(animated: false, completion: nil)
    }
}

func attempt3() {
    (view.window?.rootViewController as? UIPageViewController)?.dismiss(animated: true, completion: nil)
    self.dismiss(animated: false, completion: {
        self.viewControllerList.forEach {
            i in
            i.navigationController?.popViewController(animated: true)
        }
    })
   self.performSegue(withIdentifier: "unwindHome", sender: self)
}

Solution

  • Here were my issues:

    • On the first storyboard, my nav controller was not set to initial.

      • After setting it to initial, i then linked from the main SB to the second SB
    • Delegates var's were not set to weak

    • RxSwift was not being disposed of properly.
      • I was at first doing pub/sub via Variables as a Global. When moving the observer into the root of my UIPageViewController, upon page breakdown the observers were disposed of causing the onComplete callback to hit.