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

时间:2015-09-09 09:16:31

标签: ios swift delegates implements

[new to 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

我想

  

类ViewController:UIViewController,UIDocumentInteractionControllerDelegate {

就足够了吗?

我试过了 Self.documentInteractionControllerViewForPreview(docController)

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

2 个答案:

答案 0 :(得分:5)

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

allProduct :: [[a]] -> [[a]]

通读how delegation works。另外,请查看您的具体案例here

答案 1 :(得分:1)

对于Swift 3.0

首先扩展你的课程

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)



}

这应该呈现以下内容

enter image description here