简体   繁体   English

什么时候应该使用导航控制器?

[英]When I should use Navigation Controller?

I don't know when I should use Navigation Controller instead of use segue with normal View Controller? 我不知道何时应该使用导航控制器而不是将segue与普通的View Controller一起使用? And if use segue, which different between Modal and Push segue? 如果使用segue,Modal和Push segue之间有什么不同?

Can you give me an example? 能给我举个例子?

Short answer: Use a Navigation Controller with "show" segues only to implement DRILL DOWN behavior. 简短回答: 使用带有“show”segue的导航控制器仅实现DRILL DOWN行为。

For example, Navigation Controller → Authors → Books → Book 例如,导航控制器→作者→书籍→书籍

  • For each level below the "root" (Authors), the Navigation Controller automatically adds the title bar and back button. 对于“根”(作者)下方的每个级别,导航控制器会自动添加标题栏和后退按钮。 So on Books, the back button is automatically named "<Authors". 所以在Books上,后退按钮自动命名为“<Authors”。

  • The child View Controllers must be connected with SHOW segues -- show segues tell the Navigation Controller "this is a parent-child relationship" and cause the expected slide-in-from-the-right transition. 子视图控制器必须与SHOW segues连接 - 显示segues告诉导航控制器“这是一个父子关系”并导致预期的右上滑过渡。 (To jump outside the hierarchy, for example, Books → Login, use a modal segue instead.) (要跳到层次结构之外,例如,Books→Login,请使用模态segue。)

  • The root View Controller has a navigation bar that you can add more bar buttons to, but child View Controllers don't, because it's added automatically. 根视图控制器有一个导航栏,您可以添加更多栏按钮,但子视图控制器不会,因为它是自动添加的。

FoodTracker Example FoodTracker示例

Now the odd-seeming layout of the FoodTracker tutorial in Apple's Start Developing iOS Apps (Swift) can be explained. 现在可以解释Apple 开始开发iOS应用程序(Swift)中 FoodTracker教程的奇怪布局。 **What's up with that second nested Navigation Controller? **第二个嵌套导航控制器有什么用? It's just a simple list of meals: tap a meal to show it in Meal Detail, or tap Add to and Meal Detail becomes Add Meal. 这只是一个简单的餐点列表:点击一顿饭以在膳食细节中显示它,或点击添加到和膳食细节变成添加膳食。

FoodTracker Storyboard FoodTracker故事板

  • The first Navigation Controller makes My Meals the root of the drill-down hierarchy for any number of views "pushed" from there on (no further Navigation Controllers are needed just to do that). 第一个导航控制器使My Meals成为深入层次结构的根,用于从那里“推送”的任意数量的视图(不需要进一步的导航控制器)。

  • But, Meal Detail is used for both displaying an existing meal and adding a new meal. 但是,膳食细节用于显示现有膳食和添加新膳食。 To add a new meal, Cancel and Save buttons are needed. 要添加新餐,需要取消和保存按钮。 The second Navigation Controller allows those buttons to be added (see 3rd point above) by making Meal Detail a root. 第二个导航控制器允许通过将膳食细节设为根来添加这些按钮(参见上面的第3点)。

  • Displaying an existing meal is a push segue, but adding a meal is a modal segue (a new meal isn't a drill-down). 显示现有的膳食是推动segue,但添加膳食是一种模式segue(新的膳食不是深入研究)。 This is important: the reason Add Meal can't just be pushed is that the automatic back button ("< My Meals") becomes ambiguous: does it save or cancel? 这很重要:添加餐不能被推动的原因是自动后退按钮(“<我的餐点”)变得模棱两可:是保存还是取消?

Because "navigation" and "push" are very general terms, and because it's nice to get a free back button, it's tempting to think Navigation Controllers are to go from anywhere to anywhere, but the behavior is intended just for hierarchical traversal. 因为“导航”和“推送”是非常通用的术语,并且因为获得自由后退按钮很好,所以认为导航控制器从任何地方到任何地方都是很诱人的,但这种行为仅用于分层遍历。

(This is an old question but I was also confused about this as an iOS n00b and like the OP I still had questions.) (这是一个老问题,但我也对此作为iOS n00b感到困惑,像OP一样,我仍有疑问。)

In my experience, there is no a general rule to decide this kind of things, it depends on the usability of your future App... 根据我的经验,决定这类事情没有一般规则,这取决于你未来App的可用性......

Navigation controller helps the user to remember where they are in every moment, and how they can go back, but could not be the best thing to use if you have too many levels... And more important, if you are using a NavigationController or a TabBarController, you have a class, accessible from all the other ViewControllers where you can have general functionality or data... 导航控制器可以帮助用户记住他们在每个时刻的位置,以及他们如何返回,但如果你的级别太多,则无法使用最好的东西...更重要的是,如果你使用的是NavigationController或者一个TabBarController,你有一个类,可以从所有其他ViewControllers访问,你可以在其中拥有一般功能或数据......

The difference between the modal and push segue is that in the first you will always return to the parent ViewController, because you are only showing new information on top, while in the push one you are replacing one ViewController with other... modal和push segue之间的区别在于,在第一个中你将始终返回到父ViewController,因为你只是在顶部显示新信息,而在push中你将一个ViewController替换为其他...

You use navigation controllers when you want to enable back button functionality. 如果要启用后退按钮功能,可以使用导航控制器。 You still use 'normal' view controllers, you just embed them in a navigation controller. 您仍然使用“普通”视图控制器,只需将它们嵌入导航控制器即可。 Then, you can push view controllers and be able to go back. 然后,您可以推送视图控制器并能够返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM