I'm trying to understand a behavior of navigating between ViewControllers with (and without) using a NavigationController and I'm misunderstanding some things while reading articles and docs so I decided to ask them.
Main question is: What happened if we have multiple NavigationControllers in Storyboard and want to go from one to another? (And this can be achieved just using segues as we do between common VCs, am I right?)
As I understand, a NavigationController represents a stack of ViewControllers within which we can pop and push these VCs. So now we change our "location" from VCs of the first NavigationController to VCs from the second one, what happens next? The first stack disappeared and now we work only within the second one? If so, does it means that the VCs stack of the first NavigationController was deleted from memory or not?
Maybe I completely misunderstand something or maybe not:). I will be happy to see your responses and hope to ask you more detail questions about navigation mechanics.
UPDATE
The point is that: Let's say we have one (initial) VC with two buttons that represent two separate parts of the app. Next we click on the first button and go to RootVC of one NC than we go back to our initial VC hit the second button and go to the another NC. What happened with the stack of the first NC when we go back to the initial VC and what is the best way to go "outside" NC to the initial VC?
UPDATE
I'm trying to understand what happens with the memory and which VCs are in the scene at the moment and so on. Maybe it absolutely unimportant if we have some additional VCs in the scene, maybe we do need them to make switching between NCs (or just VCs) faster. So I want just understand how it actually works.
The navigation controller manages the navigation bar at the top of the interface and an optional toolbar at the bottom of the interface. The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by its child view controllers.
The main tool we use to build hierarchical applications for iPhone is UINavigationController. UINavigationController is similar to UITabBarController in that it manages, and swaps in and out, multiple content views.
Imagine you have you standard application chain where you push / pop views in initial navigation controller. Then, imagine you have different view that is not part of that chain, like a user profile, which you present as modal view:
Now the top navigation controller is initial so you start from here, while in order to use second one, you would have to access it through UIStoryboard like this (red arrow):
// Get storyboard
let storyboard = UIStoryboard(name: name, bundle: NSBundle.mainBundle())
// Get profile NC
let profileNC = storyboard.instantiateViewControllerWithIdentifier("LoginNC") as! UINavigationController
But if you really want to present profile from one part of the app so it is not modal, you can do it as well (green arrow). The only difference is that now you don't need second navigation controller - so you don't connect push segue to red NC, but to login view controller directly. If you actually try to connect NC - NC and then run it, you will get runtime exception saying that you did it wrong.
Memory
All the VC stay in memory, no matter how you present them. This also holds true for background views when you present something as modal. If you have issues with memory due to long chains, you can implement cleaning / caching logic in your controllers:
func viewWillAppear(animated: Bool) {
// Call super first
super.viewWillAppear(animated)
// Prepare UI
}
func viewWillDisappear(animated: Bool) {
// Call super first
super.viewWillAppear(animated)
// do some memory cleanup, since view will not be visible atm
}
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With