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;

}

}

相关推荐
用户870568130451 小时前
iOS 异步渲染:从 CALayer 切入的实现与优化
ios
敲代码的鱼哇1 天前
跳转原生系统设置插件 支持安卓/iOS/鸿蒙UTS组件
android·ios·harmonyos
在下历飞雨1 天前
Kuikly基础之状态管理与数据绑定:让“孤寡”计数器动起来
ios·harmonyos
在下历飞雨1 天前
Kuikly基础之Kuikly DSL基础组件实战:构建青蛙主界面
ios·harmonyos
鹏多多.1 天前
flutter-使用fluttertoast制作丰富的高颜值toast
android·前端·flutter·ios
Digitally1 天前
如何在 iPhone 或 iPad 上删除文件
cocoa·iphone·ipad
他们都不看好你,偏偏你最不争气1 天前
【iOS】多界面传值
ios
MaoJiu2 天前
Flutter混合开发:在iOS工程中嵌入Flutter Module
flutter·ios
2501_915921432 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008892 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview