简体   繁体   中英

QuickLook to preview files in an modal with UINavigationController

I would like to use Quicklook to preview some documents in an app.

There is an array of fileUrls, and i would like to display all the filenames in an UITableView. When the user clicks on an fileName, ill load the selected file.

The problem here is, that the delegate fires automatically when the viewDidLoad, and checks for the selected file, But ill want to push the Quicklook-Controller to my existing UINavigationController.

When ill try with:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    currentFile = TaskFiles[indexPath.row]
    self.navigationController?.pushViewController(preview, animated: false)
}

func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
    return 1
}

func documentsDirectoryURL() -> NSURL {
    let manager = NSFileManager.defaultManager()
    let URLs = manager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    return URLs[0]
}

func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem {
    return documentsDirectoryURL().URLByAppendingPathComponent(currentFile!.url!)
}

It crashes, because the file is (of course) not selected. When ill use an additional viewController, and push the currentFile to the new ViewController, and open the file on ViewDidLoad with:

    let preview = QLPreviewController()
    presentViewController(preview, animated: true, completion: nil)

The Controller is presented fullscreen, but ill prefer to show my file in my existing UINavigationController. Is there a way to accomplish that? Do i need an additional ViewController, or can ill simple push a file to my Quicklookcontroller IN my UINavigationcontroller?

Update:

This is my ViewDidLoad:

class ModalFormCaptureFilePreviewViewController: UIViewController,     QLPreviewControllerDataSource, QLPreviewControllerDelegate {

var currentFile:Files?
let preview = QLPreviewController()

override func viewDidLoad() {
    super.viewDidLoad()
    preview.dataSource = self
    showViewController(preview, sender: self)
}
let preview = QLPreviewController()
showViewController(preview, sender: self)

will work, as presentViewController uses the modalPresentationStyle

Edited

This should work

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    currentFile = TaskFiles[indexPath.row]
    let preview = QLPreviewController()
    preview.dataSource = self
    self.navigationController?.pushViewController(preview, animated: false)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM