Asim Iftikhar Abbasi
Asim Iftikhar Abbasi

Reputation: 617

Changing rootViewController causing weird behavior

I am setting rootViewController like this in my app.

func setupMainView() {
    let rootViewContorller = window?.rootViewController

    if (rootViewContorller?.presentedViewController != nil || rootViewContorller?.presentingViewController != nil) {
        rootViewContorller?.dismiss(animated: false, completion: nil)
    }

    let tabbarController = UITabBarController()
    tabbarController.delegate = self

    let homeViewController = HomeViewController()
    let rewardsViewController = RewardsViewController()

    let homeNVc = UINavigationController()
    let rewardsNVc = UINavigationController()

    homeNVc.viewControllers = [homeViewController]
    rewardsNVc.viewControllers = [rewardsViewController]

    tabbarController.viewControllers = []

    tabbarController.viewControllers = [homeNVc, rewardsNVc]
    tabbarController.selectedIndex = 0

    self.window?.rootViewController = tabbarController
}

It is working fine. But I have to change rootViewController in the app like after registration etc. After that When I go to Debug View Hierarchy . I still see the registrationViewController there. And lets say If I change rootViewController 3-4 times all previous controllers are still there. So my question is How can I remove all viewControllers from memory before changing the rootViewController.

Upvotes: 0

Views: 269

Answers (1)

MageNative
MageNative

Reputation: 702

You can call this function this will clear all viewcontrollers.

self.window?.rootViewController?.dismissViewControllerAnimated(false, completion: nil)

Upvotes: 1

Related Questions