ios - 警告 : attempt to present whose view is not in the window hierarchy

标签 ios swift ios9 uidocumentinteraction uivewcontroller

我在 UIViewController 中有一个 UITabView,所有选项卡项都链接到其他 UIViewController。我编写了一个通过互联网下载文件的快速代码。当我选择第二个 tabItem 时,此代码运行良好,它会下载并预览下载的文件,然后当我单击第一个 tabItem 时,然后再次单击第二个 tabItem ;文件下载得很好,但它没有显示任何预览,而是 xCode 给了我一条警告消息: 我想要的是当我再次单击第二个选项卡项时,下载文件和预览文件都应该起作用。无论代码是什么。

warning: attempt to present QLPreviewController on KPIViewController whose view is not in the window hierarchy

网上找了很多解决办法,但都没有效果

第一个解决方案表示使用

 let viewer = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: path))
 UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(viewer, animated: true, completion: nil)

但是这个函数

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(viewer, animated: true, completion: nil)

不接受 UIDocumentInteractionController

第二个解决方案表示将现有的 presentViewController 函数覆盖为

override func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) {

        let APP_DELEGATE = UIApplication.sharedApplication().delegate
        var presentedModalVC:UIViewController = (APP_DELEGATE!.window?!.rootViewController?.presentedViewController)!

        if presentedModalVC == true {

            while((presentedModalVC.presentedViewController) != nil){

                presentedModalVC = presentedModalVC.presentedViewController!
            }
            presentedModalVC.presentViewController(viewControllerToPresent, animated: flag, completion: nil)
        }
        else{
            APP_DELEGATE?.window!!.rootViewController?.presentViewController(viewControllerToPresent, animated: flag, completion: nil)
        }
    }

我尝试了这个,但它的参数中还需要一个 UIViewController,其中我有 UIDocumentInteractionController 我知道这些函数不能接受 UIDocumentInteractionController 类型的 viewController。

这是我的整个快速代码:

//  KPIViewController.swift
//  download
//
//  Created by me on 15/03/2016.
//  Copyright © 2016 me. All rights reserved.
//

import UIKit

class KPIViewController: UIViewController,UITabBarDelegate, NSURLSessionDownloadDelegate, UIDocumentInteractionControllerDelegate{

    @IBOutlet weak var tabBar1: UITabBar!
    @IBOutlet weak var login_Item: UITabBarItem!
    @IBOutlet weak var QAreport_Item: UITabBarItem!


    @IBOutlet weak var KpiWebView: UIWebView!
    @IBOutlet weak var progressView: UIProgressView!

    var downloadTask: NSURLSessionDownloadTask!
    var backgroundSession: NSURLSession!

    var downloadReport:Bool!

    var AuditCodeOfDashboardCell:String?
    var AuditCodeForPDF:String?
    let isDirectory: ObjCBool = false


    override func viewDidLoad() {
        super.viewDidLoad()

        self.progressView.hidden = true
        downloadReport = false

        // Do any additional setup after loading the view.
        self.tabBar1.delegate = self

    }

    override func viewDidAppear(animated: Bool) {

        self.progressView.hidden = true

        downloadReport = false

        let backgroundSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("backgroundSession")
        backgroundSession = NSURLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
        progressView.setProgress(0.0, animated: false)


        var requestURL = NSURL!()
        var request = NSURLRequest!()
        // loading data from web

        if AuditCodeOfDashboardCell != nil{
            print(self.AuditCodeOfDashboardCell)
            requestURL = NSURL(string:“my URL string&\(AuditCodeOfDashboardCell)”)
            request = NSURLRequest(URL: requestURL!)
            AuditCodeForPDF = AuditCodeOfDashboardCell
            AuditCodeOfDashboardCell = nil

        }else{
            requestURL = NSURL(string:“my URL string”)
            request = NSURLRequest(URL: requestURL!)
        }
        KpiWebView.loadRequest(request)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

        print("selected tabItem: \(item.tag)")
        switch (item.tag) {
        case 1:
            let loginVC = self.storyboard!.instantiateViewControllerWithIdentifier("loginViewController") as! LoginView
            presentViewController(loginVC, animated: true, completion: nil)

            break
        case 2:
            if AuditCodeForPDF != nil{

                downloadReport = true

                let url = NSURL(string: “my URL string&\(AuditCodeForPDF)”)!
                urlToDownload = url
            }

//                if let resultController = storyboard!.instantiateViewControllerWithIdentifier(“2”) as? QAReportViewController {

//                    presentViewController(resultController, animated: true, completion: nil)
//                }

            break

        default:
            break
        }

        if downloadReport == true{

            let url = NSURL(string: “my URL string&\(AuditCodeForPDF)”)!

            downloadTask = backgroundSession.downloadTaskWithURL(url)
            self.progressView.hidden = false
            downloadTask.resume()
            downloadReport = false

        }
    }

    // - - Handling download file-  - - - - - -  - -

    func URLSession(session: NSURLSession,
        downloadTask: NSURLSessionDownloadTask,
        didFinishDownloadingToURL location: NSURL){

            let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
            let documentDirectoryPath:String = path.first!
            let fileManager = NSFileManager()
            var destinationURLForFile = NSURL(fileURLWithPath: documentDirectoryPath.stringByAppendingString("/Report.pdf"))

            if fileManager.fileExistsAtPath(destinationURLForFile.path!){
                //                showFileWithPath(destinationURLForFile.path!)
                do{
                    try fileManager.removeItemAtPath(destinationURLForFile.path!)
                    destinationURLForFile = NSURL(fileURLWithPath: documentDirectoryPath.stringByAppendingString("/Report.pdf"))
                }catch{
                    print(error)
                }
            }

            do {
                try fileManager.moveItemAtURL(location, toURL: destinationURLForFile)
                // show file
                dispatch_async(dispatch_get_main_queue(), { () -> Void in

                    self.showFileWithPath(destinationURLForFile.path!)
                })
            }catch{
                print("An error occurred while moving file to destination url")
            }

    }

    func showFileWithPath(path: String){
        let isFileFound:Bool? = NSFileManager.defaultManager().fileExistsAtPath(path)
        if isFileFound == true{
            dispatch_async(dispatch_get_main_queue(), { () -> Void in

                let viewer = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: path))
                viewer.delegate = self
                viewer.presentPreviewAnimated(true)
            })

        }
    }
    func URLSession(session: NSURLSession,
        downloadTask: NSURLSessionDownloadTask,
        didWriteData bytesWritten: Int64,
        totalBytesWritten: Int64,
        totalBytesExpectedToWrite: Int64){
            progressView.setProgress(Float(totalBytesWritten)/Float(totalBytesExpectedToWrite), animated: true)
    }

    func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController{
        return self

    }
    func documentInteractionControllerDidEndPreview(controller: UIDocumentInteractionController) {
        print("document preview ends")

    }

}

我找不到任何合适的解决方案来解决我的问题。我是快速的新手 请任何人帮助我。提前致谢

最佳答案

UIDocumentInteractionController 不是一种UIViewController。因此,您无法使用 presentViewController: 方法呈现 UIDocumentInteractionController

结账 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/

您可以使用 UIDocumentInteractionController 呈现文档预览或选项菜单。

关于ios - 警告 : attempt to present whose view is not in the window hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36456729/

相关文章:

ios - 谷歌登录服务器授权码 nil iOS?

iphone - 使用 iCarousel 缩放项目

ios - animateWithDuration 播放声音不重复

swift - 如何使用 HStack 使 SwiftUI View 中的所有 View 具有相同的高度?

ios - 设备上的图像尺寸错误

ios - 如果系统语言不同,如何获取英文地标位置数据?

ios - RPPreviewViewController 的共享扩展因 ReplayKit 而失效

ios - 如何在 Swift 中将核心数据查询集转换为可变数组?

ios - 从 UITableViewCell 到 DetailViewController 的 segue 不起作用

ios - 文本顶部对齐的无限大小的多行标签?