ios - 多个NavigationControllers之间的导航 [英] ios - Navigation between multiple NavigationControllers

查看:115
本文介绍了ios - 多个NavigationControllers之间的导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解使用NavigationController在(和没有)ViewControllers之间导航的行为,我在阅读文章和文档时误解了一些内容,所以我决定问他们。

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.

主要问题是:如果我们在Storyboard中有多个NavigationControllers并希望从一个到另一个,会发生什么? (这可以通过我们在常见的VC之间使用segue来实现,我是对的吗?)

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?)

据我所知,NavigationController表示一堆ViewControllers,我们可以在其中弹出并推送这些VC。所以现在我们将第一个NavigationController的VC的位置从第二个改为VC,接下来会发生什么?第一个堆栈消失了,现在我们只在第二个堆栈内工作了?如果是这样,是否意味着第一个NavigationController的VC堆栈是否已从内存中删除?

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.

更新

重点在于:假设我们有一个(初始)VC,其中两个按钮代表应用程序的两个独立部分。接下来我们点击第一个按钮然后转到一个NC的RootVC,而不是我们回到初始VC按下第二个按钮然后转到另一个NC。当我们回到最初的VC时,第一个NC的堆栈发生了什么?在外部NC到最初的VC的最佳方式是什么?

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

我正在尝试了解内存会发生什么,以及当前场景中哪些VC等等。如果我们在场景中有一些额外的VC,也许它绝对不重要,也许我们确实需要它们更快地在NC(或只是VC)之间切换。所以我想了解它是如何工作的。

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.

推荐答案

想象一下,你有标准的应用程序链,你可以在其中推送/弹出视图导航控制器。然后,假设您有不同的视图,不属于该链,就像用户个人资料一样,您将其视为模态视图:

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:

现在顶级导航控制器是初始的,所以你从这里开始,而为了使用第二个,你必须通过这样的UIStoryboard访问它(红色箭头):

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

但如果你真的想要从应用程序的某个部分呈现个人资料,那么它不是模态的,你也可以这样做(绿色箭头)。唯一的区别是,现在您不需要第二个导航控制器 - 所以您不要将push segue连接到红色NC,而是直接登录视图控制器。如果你真的尝试连接NC-NC然后运行它,你会得到运行时异常,说你做错了。

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.

内存

无论你如何呈现它们,所有VC都会留在内存中。当您将某些内容呈现为模态时,这也适用于背景视图。如果由于长链存在内存问题,可以在控制器中实现清理/缓存逻辑:

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
}

希望有所帮助!

这篇关于ios - 多个NavigationControllers之间的导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆