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;

}

}

相关推荐
2501_9159184110 小时前
掌握 iOS 26 App 运行状况,多工具协作下的监控策略
android·ios·小程序·https·uni-app·iphone·webview
2501_9159090613 小时前
iOS 混淆实战,多工具组合完成 IPA 混淆与加固(源码 + 成品 + 运维一体化方案)
android·运维·ios·小程序·uni-app·iphone·webview
非专业程序员Ping21 小时前
新手小白也能看懂的LLDB技巧/逆向技巧
ios·assembly
QWQ___qwq1 天前
SwiftUI 布局之美:Padding 让界面呼吸感拉满
ios·swiftui·swift
00后程序员张1 天前
HTTPS 包 抓取与分析实战,从抓包到解密、故障定位与真机取证
网络协议·http·ios·小程序·https·uni-app·iphone
2501_915921431 天前
iOS混淆与IPA加固实战手记,如何构建苹果应用防反编译体系
android·macos·ios·小程序·uni-app·cocoa·iphone
用户091 天前
SwiftUI 键盘快捷键作用域深度解析
ios·面试·swiftui
用户091 天前
Xcode 26 的10个新特性解析
ios·面试·swift