我们在非视图类中想要随时展示一个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的更多相关文章

  1. 【IOS学习基础】NSObject.h学习

    一.<NSObject>协议和代理模式 1.在NSObject.h头文件中,我们可以看到 // NSObject类是默认遵守<NSObject>协议的 @interface N ...

  2. 【原】iOS学习之NSDate在项目中的一些类目扩展

    在项目中,我们可能会面对各种各样的对于时间的需求,在这里提供几种可能会用到的需求代码 1.与今天的时间做比较,返回日期差值 代码: - (NSInteger)compareWithToday { NS ...

  3. iOS 学习笔记六 【APP中的文字和APP名字的国际化多语言处理】

    今天为新手解决下APP中的文字和APP名字的国际化多语言处理, 不多说了,直接上步骤: 1.打开你的项目,单机project名字,选中project,直接看图吧: 2.创建Localizable.st ...

  4. iOS开发——获取当前屏幕显示的viewcontroller

    获取当前屏幕显示的viewcontroller,然后想怎么跳就怎么跳. - (UIViewController *)getCurrentVC { UIViewController *result = ...

  5. iOS学习——获取当前最顶层的ViewController

    在iOS开发过程中,我们经常性会需要获取当前页面的ViewController,然后利用ViewController进行一些操作,例如在最顶层的ViewController上展示一个UIAlertCo ...

  6. 原 iOS深入学习(Block全面分析)http://my.oschina.net/leejan97/blog/268536

    原 iOS深入学习(Block全面分析) 发表于1年前(2014-05-24 16:45)   阅读(26949) | 评论(14) 39人收藏此文章, 我要收藏 赞21 12月12日北京OSC源创会 ...

  7. iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决

    最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...

  8. 【原】iOS学习之事件处理的原理

    在iOS学习23之事件处理中,小编详细的介绍了事件处理,在这里小编叙述一下它的相关原理 1.UITouch对象 在触摸事件的处理方法中都会有一个存放着UITouch对象的集合,这个参数有什么用呢? ( ...

  9. 【原】iOS学习47之第三方-FMDB

    将 CocoaPods 安装后,按照 CocoaPods 的使用说明就可以将 FMDB 第三方集成到工程中,具体请看博客iOS学习46之第三方CocoaPods的安装和使用(通用方法) 1. FMDB ...

随机推荐

  1. 自定义UIDatePikerView

    1.添加文件GoYearMonthDayPickerView.h .m .xib.NSDate+Helper.h .m.iCarousel.h .m 2.在Lable上显示日期 UILabel *ag ...

  2. nfs 是Network File System 网络文件系统

    NFS的基本原刚是容许不同的客户端及服务通过一组PRC分享相同的文件系统,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享.NFS在文件传送过程中依赖于RPC协议.远程过程调用Rem ...

  3. Linux系统入门学习:在curl中设置自定义的HTTP头

    http://www.linuxidc.com/Linux/2015-02/114220.htm

  4. MyBatis 特殊字符处理

    http://blog.csdn.net/zheng0518/article/details/10449549

  5. android 入门-git之上传本地代码到github

    github部分: 1.首先去github网站 上注册一个用户 2.说明 https://guides.github.com/activities/hello-world/ 2.点击 New repo ...

  6. wp8 入门到精通 Utilities类 本地存储+异步

    public class CCSetting { public async static void AddOrUpdateValue<T>(string key, T value) { t ...

  7. 第二十五篇:在SOUI中做事件分发处理

    不同的SOUI控件可以产生不同的事件.SOUI系统中提供了两种事件处理方式:事件订阅 + 事件处理映射表(参见第八篇:SOUI中控件事件的响应) 事件订阅由于直接将事件及事件处理函数连接,不存在事件分 ...

  8. 无法打开包括文件:“windows.h”: No such file or directory

      VS2012 出现如下错误: 无法打开包括文件:"windows.h": No such file or directory   解决办法,将 C:\Program Files ...

  9. css中box-shadow阴影效果的使用

    text-shadow是给文本添加阴影效果,box-shadow是给元素块添加周边阴影效果.随着HTML5和CSS3的普及,这一特殊效果使用越来越普遍. 基本语法是{box-shadow:[inset ...

  10. json方式的面向对象

    json方式只适合于一个对象. var p1 = { name:"香菇", sex :"女", dreamdu: { URL: "www.dreamd ...