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;

}

相关推荐
Zender Han15 小时前
Flutter Bloc / Cubit 最新详解与实战指南(2026版)
android·flutter·ios
tangweiguo0305198719 小时前
iOS vs Android 开发对照手册
android·ios
weixin_4434785120 小时前
flutter组件学习之Cupertino 组件(iOS风格)
学习·flutter·ios
Andy_GF1 天前
iOS26 系统适配-直接隐藏 UIBarButtonItem 的 Liquid glass 效果
ios
wjm0410062 天前
ios学习路线-- swift基础2
学习·ios·swift
开心就好20252 天前
全面解析WhatsApp Web抓包:原理、工具与安全
后端·ios
程序喵大人2 天前
OpenClaw的成功是AI Agent的“iPhone时刻”吗?
人工智能·ios·iphone·openclaw
marteker2 天前
苹果的冰球营销:用iPad设计面具,讲述科技之外的情感故事
科技·ios·ipad
H_老邪2 天前
贪心算法的应用
算法·ios·贪心算法
UXbot2 天前
APP原型生成工具测评
android·前端·人工智能·低代码·ios·开发·app原型