尝试显示其视图不在窗口层次结构中的vc [英] Attempt to present vc whose view is not in the window hierarchy

查看:94
本文介绍了尝试显示其视图不在窗口层次结构中的vc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在线程中打开文件,这是我的代码:

I'm trying to open file in thread and here is my code:

DispatchQueue.main.async(execute: { () -> Void in
    var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).appendPathComponent("File.pdf")
    self.docController = UIDocumentInteractionController.init(url: documentsURL as URL)
    self.docController?.delegate = self as? UIDocumentInteractionControllerDelegate
    self.docController?.presentPreview(animated: true)
    self.docController?.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
})

移至主屏幕时,显示此警告,并且文件未打开

when move to main screen this warning is displayed and file not open

Warning: Attempt to present <_UIDocumentActivityViewController: 0x...> on <HCM.PrintVacationDecisionVC: 0x...> whose view is not in the window hierarchy! 

有什么解决这个问题的帮助吗?

Any help to solve this problem?

推荐答案

在您的应用程序中添加给定的扩展名,并在您要呈现任何视图控制器的任何地方使用它,它对我有用,希望对您有所帮助.

Add extention given bellow to your application and use it any where you want to present any view controller, it works for me hope it helps you.

//MARK: - UIApplication Extension
extension UIApplication {
    class func topViewController(viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let nav = viewController as? UINavigationController {
            return topViewController(viewController: nav.visibleViewController)
        }
        if let tab = viewController as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(viewController: selected)
            }
        }
        if let presented = viewController?.presentedViewController {
            return topViewController(viewController: presented)
        }
        return viewController
    }
}

并通过以下代码显示它:

And Present it by following code:

 UIApplication.topViewController()?.present(vc, animated: true, completion: nil)

这篇关于尝试显示其视图不在窗口层次结构中的vc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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