I have an app whose navigation tree is described on the attached image. Basically, I have two main screens (Home and Profile), both of which can access the settings screen. On that settings screen I can go to many different specific setting screens to change. Once the user accepts the changes in any of those screens, it goes back to the main settings screen.

The expected behavior of the back button is the following:
- From the Settings screen, go back to Home or Profile, depending how the user got to the settings screen in the first place.
- From any of the specific
S{x}screens it should go back to the main Settings screen.
I’m currently using the default back behavior, but I have issues on the following case:
Home -> Settings -> S1 -> Settings -> S1 -> Settings
If the user presses the Back button, I would like it to go back to the Home screen, but it goes back to S1, and then back to Settings, and then back to S1 and so on, depending on how many times I entered the S1 screen.
How can I make the Settings view to “remember” to which screen it should go back to?
Don’t manage your view controller hierarchy manually. Use
UINavigationController, this is what it is designed for. When you need to go from your home screen to your settings screen, instantiate your settings view controller and push it onto the stack. When you need to go from your settings screen to S1, instantiate your S1 controller and push it onto the stack. When you need to come back out from S1 to your settings screen, pop it off the stack. When you need to come back out from your settings screen to your home screen, pop it off the stack.