如何在AppDelegate中显示当前显示的UIViewController? [英] How to get the current displaying UIViewController not in AppDelegate?

查看:141
本文介绍了如何在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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆