iOS获取当前正在显示的UIViewController

本文详细介绍了如何在iOS中获取当前显示的UIViewController。通过分析visibleViewController、topViewController、presentedViewController和presentingViewController等关键概念,提供了一种在不同场景下(包括导航栈和present操作)获取当前VC的方法,并给出了具体的实现代码。同时,提醒开发者注意处理presentedViewController的特殊情况。
摘要由CSDN通过智能技术生成

需求

获取当前显示view的Controller

几个概念

visibleViewController 和 topViewController

topViewController代表当前navigation栈中最上层的VC

visibleViewController代表当前可见的VC,它可能是topViewController,也可能是当前topViewController present出来的VC

presentedViewController 和 presentingViewController

presentedViewController:The view controller that is presented by this view controlller(read-only),被本视图控制器present出来的的视图控制器presentingViewController:The view controller that presented this view controller. (read-only),present出来本视图控制器的视图控制器
例如:如A–>弹出B, 则A.presentedViewController = B
B.presentingViewController = A

思路

  1. APP的架构通常是 UITabBarViewController -> UINavigationController -> UIViewController
  2. 最后获取UINavigationController的visibleViewController
  3. present的情况(包括多次present)

代码

- (UIViewController *)getCurrentVC
{
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值