iOS --- 获取屏幕顶层的UIViewController

本文介绍了在iOS开发中如何获取屏幕最顶层的UIViewController。分别讲述了获取rootViewController和currentViewController的方法,rootViewController是窗口的内容视图控制器,而currentViewController通常用于获取显示在屏幕最上层的ViewController。
摘要由CSDN通过智能技术生成

经常会遇到一些场景, 要通过代码获取到当前显示在屏幕最顶层的UIViewController. 如何获取呢?

获取rootViewController

The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

rootViewController的设定通常会在设置keyWindow的时候遇到, 表示该UIWindow的最底层的UIViewController.

UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController = keyWindow.rootViewController;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值