0
votes

I have a storyboard setup like this:

Home Nav Controller - VC1 - VC2 - VC3 - VC4

Settings Nav Controller - VC1 - VC2 - VC3

To go between Home screens and Settings screens, I have a home icon/button in Settings VC1; and a settings icon/button in Home VC1. But I am not able to get this transition work correctly.

First I tried this code.

@IBAction func homeButtonTapped(button: UIButton) {
    var navController: AppStoryboard.Main.instance.instantiateViewController(withIdentifier: "HomeNavController") as? UINavigationController

    currentViewController?.present(navController!, animated: true, completion: nil)
}

//... similar code for settingsButtonTapped

It works fine. But this creates multiple view controller instances (checked Debug Memory Graph). Say I go back and forth between Home VC1 and Settings VC1 5 times, I see 5 instances of each view controller.

Then I tried this.

@IBAction func homeButtonTapped(button: UIButton) {
    var navController: AppStoryboard.Main.instance.instantiateViewController(withIdentifier: "HomeNavController") as? UINavigationController

    window?.rootViewController = navController!
}

//... similar code for settingsButtonTapped

This fixed the problem of multiple instances. But it messes up the custom animation popups I have. The popups work fine if I never ever click the Home or Settings button. But if I click the button once, from that points onwards the popups are not displayed correctly. Say I go from VC1 to VC2, and then open a popup in VC2. The popup appears, but the background is not greyed out like it usually is, and I can see the contents of VC2 clearly, and behind that I can actually see contents of VC1 too.

Any suggestion on how to get this working?

Thanks.

2

2 Answers

1
votes

You should declare wich of the two is visible first so if we assume that Home is the first one showed to users, pressing on settings you should present it like this

@IBAction func settingsButtonTapped(button: UIButton) {
    let navController = // Instantiate your settings controller
    present(navController!, animated: true, completion: nil)
}

Then if you want to go back to the home view controller you should dismiss the settings view controller like this:

@IBAction func homeButtonTapped(button: UIButton) {
    presentingViewController?.dismiss(animated: true, completion: nil)
}

Every time you present a view controller this is displayed above the view controller currently displayed and it covers it but the first view controller is not removed and deallocated.

0
votes

You should only present one Navigation Controller and i think it should be the SettingViewController

@IBAction func SettingButtonTapped(button: UIButton) {
    var navController: AppStoryboard.Main.instance.instantiateViewController(withIdentifier: "SettingNavController") as? UINavigationController

    currentViewController?.present(navController!, animated: true, completion: nil)
}

But in homeButtonTapped you should just dismiss the controller

@IBAction func homeButtonTapped(button: UIButton) {
   self.navigationController?.dismiss(animated: true, completion: nil)
}