迅速显示QLPreviewPanel

时间:2018-09-16 09:25:37

标签: swift macos preview

我想使用QLPreviewPanel预览一些文件。

我使用情节提要添加了以下ViewController

class ViewController: NSViewController, QLPreviewPanelDataSource, QLPreviewPanelDelegate {
override func viewDidAppear() {
    super.viewDidAppear()

    self.nextResponder = MainWindowController.testinstance!.nextResponder
}

@IBAction func btn(_ sender: Any) {
    openPreview(url: URL(fileURLWithPath: "/Users/usr/Desktop/test.mp3"))
}

//preview for audio
private var previewURL : URL?
func openPreview(url: URL){
    previewURL = url
    if let sharedPanel = QLPreviewPanel.shared() {
        sharedPanel.delegate = self
        sharedPanel.dataSource = self
        sharedPanel.makeKeyAndOrderFront(nil)
    }
}

func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
    return 1
}

func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
    if previewURL == nil {
        return nil
    }
    return previewURL as? QLPreviewItem
}

override func acceptsPreviewPanelControl(_ panel: QLPreviewPanel!) -> Bool {
    return true
}

override func beginPreviewPanelControl(_ panel: QLPreviewPanel!) {
    panel.dataSource = self
    panel.delegate = self
}

override func endPreviewPanelControl(_ panel: QLPreviewPanel!) {
    panel.dataSource = nil
    panel.delegate = nil
}}

一切正常-但是我得到了错误 [QL] QLError(): -[QLPreviewPanel setDelegate:] called while the panel has no controller - Fix this or this will raise soon. See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.

我该如何解决?还是只是忽略该错误而保存?

1 个答案:

答案 0 :(得分:-2)

声明一个QLPreviewController,然后尝试显示项目。

以下是使用QLPreviewController查看和打开文件的示例

Example of QLPreviewController