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;

}

相关推荐
SY.ZHOU18 小时前
移动端架构体系(四):View层的组织与调用方案
flutter·ios·架构·系统架构·安卓
inxx21 小时前
iOS 26 模拟器启动卡死:Method Swizzling 在系统回调时触发 nil 崩溃
ios
Swift社区21 小时前
鸿蒙 vs iOS / Android:谁更适合 AI?
android·ios·harmonyos
亘元有量-流量变现1 天前
ASO优化全流程实操指南:从基础到迭代,精准提升App曝光与转化
android·ios·harmonyos·aso优化·方糖试玩
zhangjikuan891 天前
iOS屏幕适配方案
ios
tangweiguo030519872 天前
SwiftUI布局完全指南:从入门到精通
ios·swift
T1an-12 天前
最右IOS岗一面
ios
坏小虎2 天前
Expo 快速创建 Android/iOS 应用开发指南
android·ios·rn·expo
光影少年2 天前
Android和iOS原生开发的基础知识对RN开发的重要性,RN打包发布时原生端需要做哪些配置?
android·前端·react native·react.js·ios
北京自在科技2 天前
Find My 修复定位 BUG,AirTag 安全再升级
ios·findmy·airtag