ios presentViewController 和 UINavigationController 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14233017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:47:57  来源:igfitidea点击:

difference between presentViewController and UINavigationController?

iosobjective-ccocoa-touchuiviewcontroller

提问by user1940888

Please tell the difference between presentViewControllerand UiNavigationController? Could I use presentViewControllerinstead of UINavigationControllerto navigate different views in the app? Whats the scenario to use either?

请告诉之间的区别presentViewControllerUiNavigationController?我可以使用presentViewController而不是UINavigationController导航应用程序中的不同视图吗?使用的场景是什么?

回答by sergio

presentViewControlleroffers a mechanism to display a so-called modal view controller; i.e., a view controller that will take full control of your UI by being superimposed on top of a presenting controller.

presentViewController提供一种机制来显示所谓的模态视图控制器;即,一个视图控制器将通过叠加在呈现控制器的顶部来完全控制您的 UI。

UINavigationControlleroffers a much more flexible mechanism where you can push a new controller, and later pop it, so to go back to the previous one, in a ordered way. Imagine that controllers in a navigation controller will just build a sequence from left to right.

UINavigationController提供了一种更灵活的机制,您可以在其中推送新控制器,然后弹出它,以便以有序的方式返回到前一个控制器。想象一下,导航控制器中的控制器只会从左到右构建一个序列。

I think that presentViewControlleris most suitable for use with just oneview controller being presented at a time. You can surely use it to stack more view controllers one on top of the other (and thus sort of "mimic" a poor-man's navigation controller), but my bet is you will quickly find something not working as you expected.

我认为这presentViewController最适合一次只显示一个视图控制器。你当然可以用它来堆叠更多的视图控制器(从而有点“模仿”穷人的导航控制器),但我敢打赌,你很快就会发现一些东西无法按预期工作。

Specifically, an example of such limitation is the following: when you dismiss a modal view controller (in order to "close" it), all of your modally presented view controllers (from the same presenting controller) will also be dismissed at once. So you simply will not be able to implement a "go back"/navigation like functionality.

具体来说,此类限制的一个示例如下:当您关闭一个模态视图控制器(为了“关闭”它)时,所有以模态呈现的视图控制器(来自同一个呈现控制器)也将同时被关闭。因此,您根本无法实现“返回”/导航之类的功能。

So, it depends on what you are trying to do.

因此,这取决于您要尝试做什么。

回答by Andrew Tetlaw

A UINavigationControlleris a subclass of UIViewControllerthat manages a stack of view controllers and adds a back button etc.

AUINavigationControllerUIViewController管理视图控制器堆栈并添加后退按钮等的子类。

presentViewControlleris a method of the UIViewControllerclass you use to present a modal view controller.

presentViewControllerUIViewController用于呈现模态视图控制器的类的方法。

回答by mgr

The UINavigationControllermaintains a navigation stack for you. You are then able to navigate through hierarchical content.

UINavigationController你保持一个导航堆栈。然后,您可以浏览分层内容。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

If you use UIViewControllerspresentViewControllermethod you are basically just replacing the view controller. no navigation stack is maintained for you.

如果您使用UIViewControllerspresentViewController方法,您基本上只是替换视图控制器。没有为您维护导航堆栈。

回答by Andy

UINavigationController is a class, presentViewController is an instance method of UIViewController (iOS 5 + ), of which UINavigationController is a subclass.

UINavigationController 是一个类,presentViewController 是 UIViewController (iOS 5+) 的一个实例方法,其中 UINavigationController 是一个子类。

pushViewController is a comparable method to presentViewController. It is an instance method of UINavigationController, for iOS 2 +

pushViewController 是与presentViewController 类似的方法。它是 UINavigationController 的一个实例方法,适用于 iOS 2 +