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;

}

相关推荐
00后程序员张1 小时前
Jenkins 自动上传 IPA 到 App Store 把发布步骤融入 CI/CD
android·ios·小程序·https·uni-app·iphone·webview
小小程序员mono1 小时前
WWDC 2026 之后,M5 桌面 Mac 上市时间预测
macos·ios·wwdc
人月神话-Lee4 小时前
WWDC26 深度解析:如何在 iOS 27 中打造“秒开”的相机体验
ios·swift·相机·wwdc·用户体验
zhaocarbon5 小时前
OC HTTP SSE客户端
http·ios·objective-c
2501_915909065 小时前
iOS IPA文件反编译与打包操作方法详解
android·ios·小程序·https·uni-app·iphone·webview
卡卡西Sensei5 小时前
2026 苹果 WWDC 完整总结
macos·ios·wwdc
健了个平_2419 小时前
iOS 27 适配笔记
ios·xcode·wwdc
Tr2e19 小时前
🐱 从 0 到 1:用 Swift 手搓一个 macOS 桌面宠物(附源码)
macos·ios·swift
iOS开发上架哦1 天前
Jenkins 自动上传 IPA 到 App Store 把发布步骤融入 CI/CD
后端·ios
ZJPRENO1 天前
2026 苹果 WWDC 完整总结
ios