【原】iOS学习之在NSObject子类中获取当前屏幕显示的ViewController
我们在非视图类中想要随时展示一个view时,需要将被展示的view加到当前view的子视图,或用当前view presentViewController,或pushViewContrller,这些操作都需要获取当前正在显示的ViewController。
代码如下:(详细理解请仔细阅读注释)
#pragma mark 获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
// 定义一个变量存放当前屏幕显示的viewcontroller
UIViewController *result = nil; // 得到当前应用程序的主要窗口
UIWindow * window = [[UIApplication sharedApplication] keyWindow]; // windowLevel是在 Z轴 方向上的窗口位置,默认值为UIWindowLevelNormal
if (window.windowLevel != UIWindowLevelNormal)
{
// 获取应用程序所有的窗口
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
// 找到程序的默认窗口(正在显示的窗口)
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
// 将关键窗口赋值为默认窗口
window = tmpWin;
break;
}
}
} // 获取窗口的当前显示视图
UIView *frontView = [[window subviews] objectAtIndex:]; // 获取视图的下一个响应者,UIView视图调用这个方法的返回值为UIViewController或它的父视图
id nextResponder = [frontView nextResponder]; // 判断显示视图的下一个响应者是否为一个UIViewController的类对象
if ([nextResponder isKindOfClass:[UIViewController class]]) {
result = nextResponder;
} else {
result = window.rootViewController;
}
return result;
}
代码说明:
- 代码中使用的 UIApplication类 属性说明:
keyWindow(@property(nonatomic, readonly) UIWindow *keyWindow)
官网描述:
The value of this property is YES when the window is the key window or NO when it is not. The key window receives keyboard and other non-touch related events. Only one window at a time may be the key window.
个人理解:
应用程序的主要窗口
windows(@property(nonatomic, readonly) NSArray <__kindof UIWindow *> *windows)
官网描述:
This property contains the UIWindow objects currently associated with the app. This list does not include windows created and managed by the system, such as the window used to display the status bar.
个人理解:
获取应用程序中显示和隐藏的所有的Window窗口,放到一个数组中
- 代码中使用的 UIWindow类 属性说明:
windowLevel(@property(nonatomic) UIWindowLevel windowLevel)
官网描述:
Window levels provide a relative grouping of windows along the z-axis. All windows assigned to the same window level appear in front of (or behind) all windows assigned to a different window level. The ordering of windows within a given window level is not guaranteed.
个人理解:
Z轴 方向上的窗口位置,默认值为UIWindowLevelNormal
- 代码中使用的 UIResponder类 中对象方法说明:
- (UIResponder *)nextResponder
官网描述:
The UIResponder class does not store or set the next responder automatically, instead returning nil by default. Subclasses must override this method to set the next responder. UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t); UIViewController implements the method by returning its view’s superview; UIWindow returns the application object, and UIApplication returns nil.
个人理解:
返回下一个响应者,也就是在执行顺序中的下一个。
【原】iOS学习之在NSObject子类中获取当前屏幕显示的ViewController的更多相关文章
- 【IOS学习基础】NSObject.h学习
一.<NSObject>协议和代理模式 1.在NSObject.h头文件中,我们可以看到 // NSObject类是默认遵守<NSObject>协议的 @interface N ...
- 【原】iOS学习之NSDate在项目中的一些类目扩展
在项目中,我们可能会面对各种各样的对于时间的需求,在这里提供几种可能会用到的需求代码 1.与今天的时间做比较,返回日期差值 代码: - (NSInteger)compareWithToday { NS ...
- iOS 学习笔记六 【APP中的文字和APP名字的国际化多语言处理】
今天为新手解决下APP中的文字和APP名字的国际化多语言处理, 不多说了,直接上步骤: 1.打开你的项目,单机project名字,选中project,直接看图吧: 2.创建Localizable.st ...
- iOS开发——获取当前屏幕显示的viewcontroller
获取当前屏幕显示的viewcontroller,然后想怎么跳就怎么跳. - (UIViewController *)getCurrentVC { UIViewController *result = ...
- iOS学习——获取当前最顶层的ViewController
在iOS开发过程中,我们经常性会需要获取当前页面的ViewController,然后利用ViewController进行一些操作,例如在最顶层的ViewController上展示一个UIAlertCo ...
- 原 iOS深入学习(Block全面分析)http://my.oschina.net/leejan97/blog/268536
原 iOS深入学习(Block全面分析) 发表于1年前(2014-05-24 16:45) 阅读(26949) | 评论(14) 39人收藏此文章, 我要收藏 赞21 12月12日北京OSC源创会 ...
- iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决
最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...
- 【原】iOS学习之事件处理的原理
在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...
- 【原】iOS学习47之第三方-FMDB
将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...
随机推荐
- python基础——匿名函数
python基础——匿名函数 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时 ...
- XMPP框架下微信项目总结(3)获取点子名片信息(个人资料)更新电子名片
思路:1 调用方法,添加点子名片模块(名片信息含电话,头像,单位个人信息)等 开启ps:APP发送请求到服务器openfire,服务器返回个人信息,app存储到数据库,app界面需要数据通过数据库获取 ...
- php单双引号
1.定义字符串 定义字符串时,只有一种引号被视为定义符,即单引号或双引号.于是,如果一个字符串由双引号开始,那么只有双引号被分析器解析.这样,你就可以在双引号串中包含任何其他字符,甚至单引号.下面的引 ...
- 跳出IFrame几种方式
1. <script type="text/javascript"> if (top.location !== self.location) { top.locatio ...
- 同一服务器配置DataGuard
实验环境:1.虚拟机VMware Server 1.0.62.操作系统:ora10g@linux5 /home/oracle$ cat /etc/redhat-releaseRed Hat Enter ...
- 跨平台C的IDE
1.JetBrains的新跨平台C++ IDE,CLion已经开始EAP了,不过这货是收费的 http://confluence.jetbrains.com/display/CLION/Early+A ...
- supervisor使用
supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量,一个很重要的功能就是监控服务器的主要后台进程,并在出现问题是自动重启. 根据服务器上的python ...
- 免费电子书:C#代码整洁之道
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:<Clean Code(代码整洁之道)>是一本经典的著作,那么对于编写整洁 ...
- C# 使用 NPOI 库读写 Excel 文件
NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...
- 细说static关键字及其应用
场景 先看段代码,考虑以下场景,其运行结果是什么? public class Test { static int i = 8; public void printI() { int i = 88; S ...