ios OC 获取当前控制器

直接贴代码,所有页面均可使用 view或者object

//拿到当前的控制器

  • (UIViewController *)getCurrentViewController {

UIViewController* currentViewController = self getRootViewController;

BOOL runLoopFind = YES;

while (runLoopFind) {

if (currentViewController.presentedViewController) {

currentViewController = currentViewController.presentedViewController;

} else {

if (currentViewController isKindOfClass:\[UINavigationController class]) {

currentViewController = ((UINavigationController *)currentViewController).visibleViewController;

} else if (currentViewController isKindOfClass:\[UITabBarController class]) {

currentViewController = ((UITabBarController* )currentViewController).selectedViewController;

} else {

break;

}

}

}

return currentViewController;

}

  • (UIViewController *)getRootViewController{

UIWindow* window = \[\[UIApplication sharedApplication delegate] window];

NSAssert(window, @"The window is empty");

return window.rootViewController;

}

相关推荐
_瑞1 天前
深入理解灵动岛
前端·ios·app
黑科技iOS上架1 天前
分享一个开源的iOS应用AppIcon生成器
经验分享·ios
wabil2 天前
【LVGL】滑动切换页面的界面优化实践
开发语言·ios·swift
威武的花瓣2 天前
调用Page.RegisterAsyncTask()的异步页
ios·iphone
Digitally3 天前
5 种简易方法:摩托罗拉手机数据迁移至 iPhone 17
ios·智能手机·iphone
EricStone5 天前
VibeCoding工程流程学习二:iOS项目架构
ios·vibecoding
天桥吴彦祖7 天前
判断iOS如何监听手机屏幕是否锁屏
ios
敲代码的鱼8 天前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
时光足迹8 天前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
时光足迹8 天前
JPush UniApp UTS 插件完全参考手册:API、事件与厂商通道一网打尽
vue.js·ios·uni-app