ios - 使用 UIWindow 获取 visibleViewController?

标签 ios swift

<分区>

所以,我有 self 作为 UIWindow,但是我怎样才能在当前时刻获得 visibleViewController?

最佳答案

在 swift3 中:

func getVisibleViewController(_ rootViewController: UIViewController?) -> UIViewController? {

    var rootVC = rootViewController
    if rootVC == nil {
        rootVC = UIApplication.shared.keyWindow?.rootViewController
    }

    if rootVC?.presentedViewController == nil {
        return rootVC
    }

    if let presented = rootVC?.presentedViewController {
        if presented.isKind(of: UINavigationController.self) {
            let navigationController = presented as! UINavigationController
            return navigationController.viewControllers.last!
        }

        if presented.isKind(of: UITabBarController.self) {
            let tabBarController = presented as! UITabBarController
            return tabBarController.selectedViewController!
        }

        return getVisibleViewController(presented)
    }
    return nil
}

关于ios - 使用 UIWindow 获取 visibleViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526926/

相关文章:

ios - 使用 dispatch_async 在后台加载图像

ios - iOS 7 的核心数据、iCloud 和预构建数据

ios - 如何发布 One Signal 通知的附加数据并接收这些数据?

ios - 元组中的类型推断?

Facebook 问题的 iOS 社交框架

ios - 在 ios 7 中添加显示窗体底部的栏

ios - 在 Swift 中初始化 UITableViewCells 的最佳实践

swift - 带有 NodeJs 的 IBM Bluemix 对象存储服务的临时公共(public) URL

ios - 未能绑定(bind) EAGLDrawable iOS swift

ios - 从 URL 捕获图像时出错