ios - 尝试呈现其 View 不在窗口层次结构中的 vc

标签 ios swift swift3

<分区>

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

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)
})

当移动到主屏幕时显示此警告并且文件未打开

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

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

最佳答案

将下面给出的扩展添加到您的应用程序,并在您想要呈现任何 View Controller 的任何地方使用它,它对我有用,希望它对您有所帮助。

//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
    }
}

并通过以下代码呈现:

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

关于ios - 尝试呈现其 View 不在窗口层次结构中的 vc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45347804/

相关文章:

ios - 在 UICollectionView 中显示进度

ios - 将视频从 UITableViewController 更改为在 AVPlayerViewController 中播放

ios - 在 UITableView swift 中限制每个部分的单元格选择

ios - 根据下面的内容更改状态栏的颜色

javascript - Cardova IOS 10. "Failed to load webpage with error: The URL can’ t被显示”

javascript - SVG onclick 事件在 iOS 设备上未触发

ios - 在 UITabBarController 中的不同选项卡之间共享公共(public) View

ios - 自动代码生成 NSManagedObject 子类 Swift 3 中的无效重新声明

ios - Facebook SDK 3.1 - com.facebook.sdk 使用 [facebook 授权 :permissions] 进行身份验证时出现错误 5

ios - 在主视图顶部添加新 View (例如错误消息)时如何自动布局约束