[iOS 获取当前窗口导航控制器中栈顶viewController] 不普遍适用

news/2024/7/20 20:12:48 标签: ios, 导航, , 结构, 获取当前视图控制器

项目背景

目前做MQTT多点登陆提示,弹出框目前显示于手机当前窗口
弹出框不与当前viewController有关系

问题:我需要获取当前屏幕下的viewController

结构特殊">项目结构特殊

目前App的结构
MainViewController {
1.NavigationViewController1
2.NavigationViewController2
3.NavigationViewController3
}

(lldb) po self.childViewControllers
<__NSArrayI 0x170245f40>(
<UINavigationController: 0x12401e400>,
<UINavigationController: 0x124957600>,
<UINavigationController: 0x124808600>
)

步骤1

为了获取当前viewController  首先要获取当前的 UINavigationController

由于是 MainViewController中 UINavigationController 以数组形式存在
通过下标获取就很简单
(这里的self == MainViewController )

  //1.获取当前下标
                if (!currentVCindex) {
                    currentVCindex = 0;
                }//
                [self.childViewControllers[currentVCindex]
<UINavigationController: 0x124808600>

步骤2

获取当前 UINavigationController 下所有子控制器

[self.childViewControllers[currentVCindex] childViewControllers]
(lldb) po [self.childViewControllers[currentVCindex] childViewControllers]
<__NSArrayI 0x17002e800>(
<SandBaoViewController: 0x123d84940>,
<TransferBeginViewController: 0x123d19640>
)

步骤3

已经看到,我们获取到了子控制器,那么取到顶控制器就只要取数组最后一个

[[self.childViewControllers[currentVCindex] childViewControllers] lastObject]
(lldb) po [[self.childViewControllers[currentVCindex] childViewControllers ] lastObject]
<TransferBeginViewController: 0x123d19640>

(lldb) 

以上代码 仅做参看

网络上还有许多获取当前控制器的代码 如下:
获取当前最顶层的ViewController
http://www.cocoachina.com/ios/20161013/17739.html

iOS 获取当前正在显示的ViewController
http://blog.csdn.net/worldzhy/article/details/42120929/
http://www.jianshu.com/p/949fd3dd720b


http://www.niftyadmin.cn/n/1557337.html

相关文章

js 实现 输入 行数和列数动态生成一个表格

需求及效果 js 实现 输入 行数和列数动态生成一个表格 要求时 输入 x 和 y &#xff0c;x是行 y是列 &#xff0c;然后 输入 一个数 x1,y1对其 上色。 实现步骤 思路&#xff1a; 1生成表格.双重for 循环 第一层 渲染 tr 第二层渲染 td。 2.上色 的话 根据x1找到 tr在 根…

tp5 数据库Db查询操作

$data Db::query(select * from tf_action); $data Db::query(select * from tf_action where id > ? and id < ?,[1,10]); $sql Db::getLastSql(); 查询用query。 删除&#xff0c;增加&#xff0c;修改&#xff0c;用execute。 $data Db::table(tf_action)->s…

[转]7种常用手势UIGestureRecognizer介绍

7种常用手势UIGestureRecognizer介绍 http://blog.csdn.net/lotheve/article/details/46482989 #import "ViewController.h" interface ViewController () {UITapGestureRecognizer *_tap;UIPanGestureRecognizer *_pan;UIPinchGestureRecognizer *_pinch;UIRotat…

[iOS 密码过于简单的正则表达式]

资料” http://www.jianshu.com/p/3747466b71f9 //匹配6位顺增 String pattern "(?:0(?1)|1(?2)|2(?3)|3(?4)|4(?5)|5(?6)|6(?7)|7(?8)|8(?9)){5}\\d"; Pattern pa Pattern.compile(pattern); String mc "123456"; Matcher ma pa.matche…

react报 Relative imports outside of src/ are not supported

报错信息&#xff1a; You attempted to import xxx which falls outside of the project src/ directory.Relative imports outside of src/ are not supported.You can either move it inside src/, or add a symlink to it from projects node_modules/. 大概意思&#…

linux nginx

Nginx:高性能的HTTP Server, 反向代理, 正向代理[rootstation230 ~]# service httpd stop[rootstation230 ~]# chkconfig httpd off所需的软件&#xff1a;开发工具&#xff0c;开发库&#xff0c;openssl-devel部署Nginxpcre: 支持正则表达式&#xff0c;地址重写rewrite# tar…

[iOS KVO监控view的frame变化]

手残把 wokenshin 兄弟的回复给删除了… 不知道怎么撤销, // 这里补充下, wokenshin兄弟用Masonry修改约束后, 发现不能触发监听, 我这里也同样是该问题, 这里通过约束后再次设置_t.frame的方式, 可以出发该监听, 个人猜测监听动作是在set frame的时候才会触发. 而Masonry仅仅…

C# 之 Socket 简单入门示例

这个例子只是简单实现了如何使用 Socket 类实现面向连接的通信。 注意&#xff1a;此例子的目的只是为了说明用套接字写程序的大概思路&#xff0c;而不是实际项目中的使用程序。在这个例子中&#xff0c;实际上还有很多问题没有解决&#xff0c;如消息边界问题、端口号是否被占…