试图导出到 csv 但我如何从委托中实现?

Trying to export to csv but how do i implement from delegate?

[新手 swift] 我正在测试此功能以导出一些简单文件

    @IBAction func exportFile(delegate: UIDocumentInteractionControllerDelegate) {
        print("export csv")
       let fileName = tmpDir.stringByAppendingPathComponent("myFile.csv")
        let url: NSURL! = NSURL(fileURLWithPath: fileName)

        if url != nil {
            let docController = UIDocumentInteractionController(URL: url)
            docController.UTI = "public.comma-separated-values-text"
            docController.delegate = delegate
            docController.presentPreviewAnimated(true)

        }
}
 // Return the view controller from which the UIDocumentInteractionController will present itself.
    func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController)-> UIViewController {
        return self
    }

但是当我点击导出按钮时,我收到了消息

UIDocumentInteractionController delegate must implement documentInteractionControllerViewControllerForPreview: to allow preview

我以为

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {

够了吗?

我试过了 Self.documentInteractionControllerViewForPreview(文档控制器)

[编辑] 原来我犯了以下错误 docController.delegate = self//delegate

您需要实现以下委托方法。委托是从服务提供者到服务消费者的回调,为即将发生的动作做准备。在某些情况下,您必须提供详细信息(称为数据源)才能使用上述功能。

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

func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController!) -> UIView! {
    return self.view
}

func documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController!) -> CGRect {
    return self.view.frame
}

通读 how delegation works. Also, take a look at your specific case here

对于Swift3.0

首先扩展你的 class

UIDocumentInteractionControllerDelegate

那就只实现下面的方法

     func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {

    return self
}

调用 Pdf 查看器

 func MyViewDocumentsmethod(){

    let controladorDoc = UIDocumentInteractionController(url: PdfUrl! as URL)
    controladorDoc.delegate = self
    controladorDoc.presentPreview(animated: true)



}

这应该呈现以下内容