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
difference between presentViewController and UINavigationController?
提问by user1940888
Please tell the difference between presentViewController
and UiNavigationController
? Could I use presentViewController
instead of UINavigationController
to navigate different views in the app?
Whats the scenario to use either?
请告诉之间的区别presentViewController
和UiNavigationController
?我可以使用presentViewController
而不是UINavigationController
导航应用程序中的不同视图吗?使用的场景是什么?
回答by sergio
presentViewController
offers 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。
UINavigationController
offers 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 presentViewController
is 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 UINavigationController
is a subclass of UIViewController
that manages a stack of view controllers and adds a back button etc.
AUINavigationController
是UIViewController
管理视图控制器堆栈并添加后退按钮等的子类。
presentViewController
is a method of the UIViewController
class you use to present a modal view controller.
presentViewController
是UIViewController
用于呈现模态视图控制器的类的方法。
回答by mgr
The UINavigationController
maintains a navigation stack for you. You are then able to navigate through hierarchical content.
在UINavigationController
你保持一个导航堆栈。然后,您可以浏览分层内容。
If you use UIViewControllers
presentViewController
method you are basically just replacing the view controller. no navigation stack is maintained for you.
如果您使用UIViewControllers
presentViewController
方法,您基本上只是替换视图控制器。没有为您维护导航堆栈。
回答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 +