简体   繁体   English

尝试预览文件时无法让 QuickLook 工作

[英]Can't get QuickLook to work when trying to preview files

I am writing a macOS application with Swift using story boards.我正在使用故事板使用 Swift 编写 macOS 应用程序。 I have a NSTableView which contains files that I want the user to be able to preview via QuickLook.我有一个 NSTableView,其中包含我希望用户能够通过 QuickLook 预览的文件。

I seemingly have everything in place and my code looks very similar to what has been described here: QuickLook consumer as a delegate from an NSViewController , but I keep getting the error我似乎已经准备好了一切,我的代码看起来与这里描述的非常相似: QuickLook consumer as a delegate from an NSViewController ,但我不断收到错误

-[QLPreviewPanel setDataSource:] called while the panel has no controller - Fix this or this will raise soon.
    See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.

I've been trying to adapt the solution of above post to my situation with Swift and story boards.我一直在尝试使用 Swift 和故事板使上述帖子的解决方案适应我的情况。

The main pieces are:主要部分是:

import Quartz

class ViewController: NSViewController, QLPreviewPanelDataSource, QLPreviewPanelDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let windowNextResponder = self.view.window?.nextResponder
        self.view.window?.nextResponder = self
        self.nextResponder = windowNextResponder
    }

    // *** Quicklook stuff ***

    @IBAction func quickLookButtonAction(_ sender: Any) {
        guard qlPanel != nil else {
            return
        }

        if qlPanel!.currentController == nil {
            print ("No controller")
            //qlPanel!.windowController = self.view.window?.windowController
            // qlPanel!.updateController()
        } else {
            print (qlPanel!.currentController)
        }
        qlPanel!.delegate = self
        qlPanel!.dataSource = self

        qlPanel!.makeKeyAndOrderFront(self)
    }

    func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
        return CSVarrayController.selectedObjects.count
    }

    func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
        let file = CSVarrayController.selectedObjects[index] as! CSVfile
        return file.url as NSURL
    }
    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
    }
} 

With or without messing with the responder chain I get the error.无论是否弄乱响应者链,我都会收到错误消息。 The delegate functions all get called as expected as well.委托函数也都按预期调用。

Remove消除

qlPanel!.delegate = self
qlPanel!.dataSource = self

in quickLookButtonAction , the viewcontroller isn't in control yet.quickLookButtonActionquickLookButtonAction控制器尚未控制。 Wait for beginPreviewPanelControl .等待beginPreviewPanelControl

From the documentation for currentController :currentController的文档中:

You should never change the preview panel's state (its delegate, datasource, and so on) if you are not controlling it.如果您不控制预览面板的状态(它的委托、数据源等),则永远不要更改它。

From comments in QLPreviewPanel.h for -beginPreviewPanelControl: :来自 QLPreviewPanel.h 中-beginPreviewPanelControl:评论:

Sent to the object taking control of the Preview Panel.发送到控制预览面板的对象。

The receiver should setup the preview panel (data source, delegate, binding, etc.) here.接收方应在此处设置预览面板(数据源、委托、绑定等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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