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;

}

相关推荐
杜哥无敌3 小时前
反向代理那些事儿: MaxKB 和1Panel ,用https还是http
ios·iphone
遥不可及zzz4 小时前
iOS 27 适配指南:Xcode 27 升级、UIScene 生命周期迁移及游戏项目兼容性处理
ios·ios 27·xcode27
Geek-Chow7 小时前
02 · Redux 是什么,又不是什么
ios
楚识科技8 小时前
移动端 OCR SDK 集成拆解:Android/iOS 离线识别与结构化回填
android·ios·ocr
CocoaKier1 天前
神技分享:App加锁,App隐藏?苹果官方自带,只需要一个长按
ios·apple
传奇开心果编程1 天前
【六大移动UI框架比较学与练】第5课:交互与导航——事件处理、手势与页面路由实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
Geek-Chow1 天前
01 · 为什么要在 iOS 上用 Redux
ios
传奇开心果编程2 天前
【六大移动UI框架比较学与练】第3课:文本、图片、布局核心组件实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
艾欧饲2 天前
能力、Entitlement 与扩展:侧载的 App 为什么装上了,有的功能却不能用
ios
庚鑫2 天前
只改最低支持版本,为什么 iOS 播放器在初始化时崩了?
ios