How do I get the RootViewController from a pushed controller?(如何从推送的控制器中获取 RootViewController?)
问题描述
所以,我从 RootViewController 推送一个视图控制器,例如:
<上一页>[self.navigationController pushViewController:anotherViewController Animation:YES] ;但是,从 anotherViewController 现在,我想再次访问 RootViewController.
我在努力
<上一页>//(现在在另一个视图控制器中)///RootViewController *root = (RootViewController*)self.parentViewController ;//不.//呃RootViewController *root = (RootViewController*)[self.navigationController.viewControllers objectAtIndex:0] ;//是的!!有用我不确定为什么这样做有效,我不确定这是否是最好的方法.有人可以评论从您推送到该 RootViewController 的 navigationController 的控制器中获取 RootViewController 的更好方法以及我所做的方式是否可靠?
使用viewControllers 属性.示例代码:
//在另一个 ViewController 中NSArray *viewControllers = self.navigationController.viewControllers;UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];这是获取后退"视图控制器的标准方法.objectAtIndex:0 起作用的原因是因为您尝试访问的视图控制器也是根视图,如果您在导航中更深入,则后视图与根视图不同.
So, I push a view controller from RootViewController like:
[self.navigationController pushViewController:anotherViewController animated:YES] ;
BUT, FROM anotherViewController now, I want to access the RootViewController again.
I'm trying
// (inside anotherViewController now) ///RootViewController *root = (RootViewController*)self.parentViewController ; // No. // err RootViewController *root = (RootViewController*)[self.navigationController.viewControllers objectAtIndex:0] ; // YES!! it works
I'm not sure WHY this works and I'm not sure if its the best way to do it. Can somebody comment on a better way to get the RootViewController from a controller you've pushed into that RootViewController's navigationController and whether or not the way I've done it is reliable or not?
Use the viewControllers property of the UINavigationController. Example code:
// Inside another ViewController
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];
This is the standard way of getting the "back" view controller. The reason objectAtIndex:0 works is because the view controller you're trying to access is also the root one, if you were deeper in the navigation, the back view would not be the same as the root view.
这篇关于如何从推送的控制器中获取 RootViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从推送的控制器中获取 RootViewController?
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
