iPhone - 如何找到最顶层的视图控制器

  • (UIViewController*)topViewController {

return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];

}

  • (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {

if ([rootViewController isKindOfClass:[UITabBarController class]]) {

UITabBarController* tabBarController = (UITabBarController*)rootViewController;

return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];

}

else if ([rootViewController isKindOfClass:[UINavigationController class]]) {

UINavigationController *navigationController = (UINavigationController*)rootViewController;

return [self topViewControllerWithRootViewController:navigationController.visibleViewController];

}

else if (rootViewController.presentedViewController) {

UIViewController* presentedViewController = rootViewController.presentedViewController;

return [self topViewControllerWithRootViewController:presentedViewController];

}

else {

return rootViewController;

}

}

相关推荐
如此风景几秒前
SwiftUI 状态管理详解
ios
QuantumLeap丶7 分钟前
《Flutter全栈开发实战指南:从零到高级》- 25 -性能优化
android·flutter·ios
jumu2022 小时前
电压型虚拟同步发电机(VSG)离网仿真模型搭建之旅
iphone
游戏开发爱好者84 小时前
H5 混合应用加密 Web 资源暴露到 IPA 层防护的完整技术方案
android·前端·ios·小程序·uni-app·iphone·webview
2501_915106324 小时前
最新版本iOS系统设备管理功能全面指南
android·macos·ios·小程序·uni-app·cocoa·iphone
游戏开发爱好者84 小时前
HTTPS DDoS 排查 异常流量到抓包分析
网络协议·ios·小程序·https·uni-app·iphone·ddos
TouchWorld5 小时前
iOS逆向-哔哩哔哩增加3倍速播放(3)-[横屏视频-全屏播放]场景
ios·swift
2501_915918415 小时前
iOS 性能监控 运行时指标与系统行为的多工具协同方案
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张6 小时前
IPA 混淆技术全解,从成品包结构出发的 iOS 应用安全实践与工具组合
android·安全·ios·小程序·uni-app·cocoa·iphone