xcode iOS:如果不使用 UINavigationController,以编程方式查看导航堆栈中的视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31193774/
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
iOS: programmatically see view controllers in navigation stack if not using a UINavigationController?
提问by Crashalot
Is it possible to see which view controllers are in the navigation stack? New to Swift and iOS and just want to make sure navigation is happening properly and view controllers aren't stacking up inadvertently.
是否可以查看导航堆栈中的视图控制器?Swift 和 iOS 新手,只想确保导航正常进行,并且视图控制器不会无意中堆积起来。
To clarify, it seems like you can use the viewControllers
property in a UINavigationController
, but for learning purposes, we're not using a navigation controller and doing the navigation manually through exit segues.
澄清一下,您似乎可以viewControllers
在 a 中使用该属性UINavigationController
,但出于学习目的,我们没有使用导航控制器,而是通过 exit segues 手动进行导航。
Can you programmatically print out the view controller hierarchy if not using a UINavigationController
?
如果不使用UINavigationController
?可以以编程方式打印出视图控制器层次结构吗?
回答by Caleb
Is it possible to see which view controllers are in the navigation stack?...Can you programmatically print out the view hierarchy if not using a UINavigationController?
是否可以查看导航堆栈中的视图控制器?...如果不使用 UINavigationController,您能否以编程方式打印出视图层次结构?
You're talking about two complete different things here. Views and view controllers are not the same kind of object. They do both tend to be used in directed graphs of objects like themselves, but the graphs of those two kinds of objects are quite different. That is, a navigation controller is a kind of view controller that keeps a list of other view controllers, and (separately) a view keeps an array of the views that it contains. Finally most view controllers manage a single view that usually contains other views.
你在这里谈论两个完全不同的事情。视图和视图控制器不是同一种对象。它们都倾向于用于像它们一样的对象的有向图,但是这两种对象的图是完全不同的。也就是说,导航控制器是一种视图控制器,它保存其他视图控制器的列表,并且(单独)视图保存它包含的视图数组。最后,大多数视图控制器管理一个通常包含其他视图的视图。
An easy way to see the hierarchy of views is to set a breakpoint on some line in one of your view controllers and use the debugger to po [self.view recursiveDescription]
, which will print (in the debugger) a description of the view and subviews that that view controller manages.
查看视图层次结构的一种简单方法是在您的一个视图控制器中的某行上设置一个断点,并使用调试器来po [self.view recursiveDescription]
打印(在调试器中)该视图控制器管理的视图和子视图的描述。
An even easier way to do it is to use Xcode's nifty view debugger, which gives you a layered view that you can examine in 3D:
更简单的方法是使用 Xcode 的漂亮视图调试器,它为您提供了一个分层视图,您可以在 3D 中检查:
The icon for that inspector looks like one rectangle on top of another:
该检查器的图标看起来像一个矩形叠加在另一个矩形上:
You can certainly print out any graph of objects that your code can access, but from your comment it sounds like you'll have to write a method to do it. If you're not using a navigation controller, then there is no "navigation stack" to print out. You can implement your own version of a navigation stack, of course, but then it's up to you to provide a way to inspect it.
您当然可以打印出您的代码可以访问的任何对象图,但从您的评论看来,您必须编写一个方法才能做到这一点。如果您不使用导航控制器,则不会打印出“导航堆栈”。当然,您可以实现您自己版本的导航堆栈,但是您需要提供一种检查它的方法。
回答by rounak
UINavigationController holds its view controllers in a navigationController.viewControllers
property, which is an array. You can inspect that value to see the navigation stack.
UINavigationController 将其视图控制器保存在一个navigationController.viewControllers
属性中,该属性是一个数组。您可以检查该值以查看导航堆栈。
You could also use Xcode's view hierarchy debugger to visually see all the views in your app in 3D.
您还可以使用 Xcode 的视图层次结构调试器以 3D 形式直观地查看应用程序中的所有视图。
Another option is to use Facebook Chisel'spvc
command which prints the view controller hierarchy.
另一种选择是使用Facebook Chisel 的pvc
命令打印视图控制器层次结构。