【原】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 ...
随机推荐
- js递归
先从外层往里调,再反. 要想明白,必须明白执行过程. 如果再不理解,就看函数功能. 函数里自己调自己就是递归!
- python简介
python起源 作者Guido van Rossum,荷兰人 在创建python之初,1989年12月份,Guido只是想用编程来打发圣诞的闲暇时光.Guido也希望能有一门语言既能够像C语言那样, ...
- codevs 1702素数判定2
Miller-Rabin算法实现,但是一直被判题程序搞,输入9999999999得到的结果分明是正确的但是一直说我错 #include <cstdio> #include <cmat ...
- UVA 10192 Vacation
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- hdu 2546饭卡
用5块钱去买最贵的物品,用剩下的m-5块去买尽量多的物品 #include<stdio.h> #include<math.h> #include<vector> # ...
- Python 实现发送、抄送邮件功能
发送邮件 问题 在web.py中,如何发送邮件? 解法 在web.py中使用web.sendmail()发送邮件. web.sendmail('cookbook@webpy.org', 'user@e ...
- 关闭 Visual Studio 2013 的 Browser Link 功能
最近公司弄新项目需要用 MVC,就把 IDE 升级到了 Visual Studio 2013,在开发的时候发现有好多请求一个本地49925的端口 . 很奇怪,一开始以为是 Visual Studio ...
- Android Matrix
转自 :http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#code Matrix的数学原理 平移变换 旋转变换 缩放变换 错切 ...
- 如何做好App的引导页?(转)
http://uedc.163.com/12264.html 当你第一次打开一款应用的时候常常会看到精美的引导页设计,它们在你未使用产品之前提前告知你产品的主要功能与特点,第一次印象的好坏会极大地影响 ...
- 攻城狮在路上(肆)How tomcat works(一) 简单的web服务器
该节总共三个类:Request\Response\HttpServer---user.dir 该节的目的是实现简单web服务器对静态文件的访问.需要对请求头.请求体的格式有所了解,不然就没有 ...