0

I have a series of view controllers. One called home, followed by 10 others theat you navigate through in sequence, ie; home -> 1 -> 2 -> 3 and so on. All view controllers have a 'home' button which takes you back to the first 'home' view controller. Is there an efficient way of doing this rather than create a 10 separate segues from each VC back to 'home' VC?

Keep in mind that I want memory usage to be as efficient as possible without creating an instance of the same view controller again and again as I navigate back and forth.

Any help is greatly appreciated.

user1391152
  • 1,259
  • 3
  • 13
  • 28
  • 3
    Create an unwind `@IBAction` in the home view controller. Then any other view controller can perform an unwind segue. See http://stackoverflow.com/a/34013347/1271826. Or see Apple [Technical Note TN2298](https://developer.apple.com/library/content/technotes/tn2298/_index.html). Besides, you don't want have to segues from these other segues back to the home scene, anyway, because that sort of circular set of segues can lead to abandoned memory (i.e. you'll use up memory unnecessarily with duplicate instances of the home scene's view controller, etc.). – Rob Sep 26 '16 at 19:26
  • 1
    Or if you're pushing them onto a navigation stack you can just pop to the root view controller – pbush25 Sep 26 '16 at 19:35
  • You say `as I navigate back and forth.` so definitely your `homeVC` is embedded in `NavigationVC` and your `homeVC` is the `rootVC`. So just do `navigationController.popToRootViewController` – Santosh Sep 26 '16 at 19:37

1 Answers1

0

Thanks to everyone who commented. Based on those comments the best course of action in this instance was as follows:

Embed Home View Controller in a Navigation controller and add the following method into Home View Controller:

@IBAction func unwindToViewController(_ sender: UIStoryboardSegue) {}

Then in all storyboards with a home button, Control-drag to its respective View Controllers "exit" icon in the top bar, then select the 'unwindToViewController' method created earlier.

That will remove all VC's in the stack when navigating back to the root VC.

user1391152
  • 1,259
  • 3
  • 13
  • 28