ios 如何访问 UINavigationController 中的堆栈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6498017/
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 20:32:38  来源:igfitidea点击:

How to access the stack in UINavigationController

iphoneobjective-ciosuinavigationcontroller

提问by Dheeraj Kaveti

viewControllers

The view controllers currently on the navigation stack.

@property(nonatomic, copy) NSArray * viewControllers

Discussion

The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

Assigning a new array of view controllers to this property is equivalent to calling the setViewControllers:animated: method with the animated parameter set to NO.

视图控制器

当前在导航堆栈上的视图控制器。

@property(nonatomic, copy) NSArray * viewControllers

讨论

根视图控制器位于数组中的索引 0,后视图控制器位于索引 n-2,顶部控制器位于索引 n-1,其中 n 是数组中的项数。

为这个属性分配一个新的视图控制器数组相当于调用 setViewControllers:animated: 方法,并将动画参数设置为 NO。

I am confused how to access the the stack I have three views in the navigation controller – root view controller, sti testing location, sti map.

我很困惑如何访问我在导航控制器中有三个视图的堆栈——根视图控制器、sti 测试位置、sti 地图。

How can I access the stack?

如何访问堆栈?

回答by Convolution

UINavigationControllershave a property called viewControllersas you have stated above. Since this is an array of View Controllers, referencing a specific view controller in this hierarchy is no different than accessing any other object in an array.

UINavigationControllers有一个viewControllers如上所述的属性。由于这是一个视图控制器数组,在此层次结构中引用特定视图控制器与访问数组中的任何其他对象没有什么不同。

UIViewController *theControllerYouWant = [self.navigationController.viewControllers objectAtIndex:(theIndexOfYourViewController)];

In addition check out the Navigation Controllersarticle in the iOS Developer Library, specifically the section called 'Modifying the Navigation Stack'.

此外,请查看iOS 开发人员库中的导航控制器文章,特别是名为“修改导航堆栈”的部分。

回答by Deepak Danduprolu

Assuming you meant that your navigation controller has three view controllers, you should be able to access the navigation controller from any of three view controllers using self.navigationController.

假设你的意思是你的导航控制器有三个视图控制器,你应该能够从三个视图控制器中的任何一个使用self.navigationController.

So if you want to get the second view controller in the stack, you should do –

所以如果你想在堆栈中获得第二个视图控制器,你应该这样做——

UIViewController * viewController = [self.navigationController.viewControllers objectAtIndex:1];

This is assuming that there are at least two view controllers on the navigation controller.

这是假设导航控制器上至少有两个视图控制器。

回答by Dancreek

The array returned by the viewControllers property is the stack. They are ordered in the same sequence that they were shown. the controller at index 0 is the controller you started with. The controller you are currently looking at is the highest index. Since indexes are counted from zero the last item index will be the count (n)-1.

viewControllers 属性返回的数组是堆栈。它们的排序顺序与显示的顺序相同。索引0处的控制器是你开始控制器。您当前查看的控制器是最高索引。由于索引从零开始计数,因此最后一项索引将是计数 (n)-1。

Now you say "views" in your post. There is a difference between views and view controllers. If you are talking about multiple pieces that are all visible at the same time then you are talking about views not view controllers. The Navigation Controller is for handling multiple view controllers. If you are dealing with views than you want to access the subviews of the current view controller's view myViewController.view.subviewsThey order themselves in a similar way.

现在您在帖子中说“意见”。视图和视图控制器之间存在差异。如果您谈论的是同时可见的多个部分,那么您谈论的是视图而不是视图控制器。导航控制器用于处理多个视图控制器。如果您正在处理视图而不是想要访问当前视图控制器视图的子视图,myViewController.view.subviews它们以类似的方式对自己进行排序。