且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS:如果不使用 UINavigationController,以编程方式查看导航堆栈中的视图控制器?

更新时间:2023-02-16 09:40:28

是否可以查看导航堆栈中的视图控制器?...如果不使用 UINavigationController,您能否以编程方式打印出视图层次结构?

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?

你在这里谈论的是两个完全不同的事情.视图和视图控制器不是同一种对象.它们都倾向于用于像它们一样的对象的有向图,但是这两种对象的图是完全不同的.也就是说,导航控制器是一种视图控制器,它保存其他视图控制器的列表,并且(单独)视图保存它包含的视图数组.最后,大多数视图控制器管理一个通常包含其他视图的视图.

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.

查看视图层次结构的一种简单方法是在您的一个视图控制器中的某行上设置断点,并使用调试器po [self.view recursiveDescription],它将打印 (在调试器中)该视图控制器管理的视图和子视图的描述.

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.

一种更简单的方法是使用 Xcode 的漂亮视图调试器,它为您提供了一个分层视图,您可以在 3D 中进行检查:

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:

该检查器的图标看起来像一个矩形叠加在另一个矩形上:

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.