ios - Navigation between multiple NavigationControllers

馋奶兔 提交于 2019-12-03 06:58:08

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!

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!