如何在AppDelegate中显示当前显示的UIViewController?

编程入门 行业动态 更新时间:2024-10-10 05:17:36
本文介绍了如何在AppDelegate中显示当前显示的UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试获取当前不显示在AppDelegate中的显示 UIViewController ,但它似乎始终获得最初的顶部 UIViewController ,而不是现在的。

I'm trying to get the currently display UIViewController that is not in the AppDelegate, but it seems to always get the initial top UIViewController, and not the present one.

AppDelegate中的以下代码获取当前的 UIViewController present,但是当我在任何一个View Controllers中使用它时,同样的功能不起作用:

The following code in AppDelegate DOES get the current UIViewController present, but this same function does not work when I use it in any one of my View Controllers:

func getTopViewController() -> UIViewController { var topViewController = UIApplication.sharedApplication().delegate!.window!!.rootViewController! while (topViewController.presentedViewController != nil) { topViewController = topViewController.presentedViewController! } return topViewController }

提供上述代码作为类似问题的答案: 在AppDelegate.m屏幕上显示当前显示的UIViewController

The above code was provided as an answer in a similar question: Get the current displaying UIViewController on the screen in AppDelegate.m

无论我有多么深刻,我都只能检索出第一个最多的View Controller。

No matter how deep down I segue to, I can only retrieve the first-most View Controller.

怎么能我得到当前的呈现 UIViewController ?

How can I get the current presenting UIViewController?

FYI:我不使用 UINavigationController ,只需常规 UIViewController 类。

FYI: I'm NOT using a UINavigationController, just regular UIViewController classes.

推荐答案

我不喜欢使用它,但有时候这是必要的。

I do not like using this but sometimes it is necessary.

static func getTopViewController() -> UIViewController { var viewController = UIViewController() if let vc = UIApplication.shared.delegate?.window??.rootViewController { viewController = vc var presented = vc while let top = presented.presentedViewController { presented = top viewController = top } } return viewController }

**编辑:

这是一个改进的版本,它将始终获得最顶级的视图控制器

Here is an improved version, it will always get top most view controller

static var top: UIViewController? { get { return topViewController() } } static var root: UIViewController? { get { return UIApplication.shared.delegate?.window??.rootViewController } } static func topViewController(from viewController: UIViewController? = UIViewController.root) -> UIViewController? { if let tabBarViewController = viewController as? UITabBarController { return topViewController(from: tabBarViewController.selectedViewController) } else if let navigationController = viewController as? UINavigationController { return topViewController(from: navigationController.visibleViewController) } else if let presentedViewController = viewController?.presentedViewController { return topViewController(from: presentedViewController) } else { return viewController } }

更多推荐

如何在AppDelegate中显示当前显示的UIViewController?

本文发布于:2023-11-08 20:50:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   AppDelegate   UIViewController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!