ios - Navigation between multiple NavigationContro

2019-03-19 07:45发布

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.

2条回答
不美不萌又怎样
2楼-- · 2019-03-19 08:43

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:

enter image description here

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!

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-19 08:48

What makes sense, is to present a new UINavigationController with its child view controller(s) from an existing one as a modal dialog (this can be done with a modal segue). Each navigation controller has its own stack, and while you're busy in the dialog, the 'master' stack remains intact. When you dismiss the dialog, you will return to the 'master'.

I'm not sure if it is technically possible to push a navigation controller onto an existing one. It makes no sense, though.

查看更多
登录 后发表回答