2015-09-09 46 views
3

[新SWIFT]我測試這個功能導出一些簡單的文件試圖導出到CSV,但我如何從委託實現?

@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)

[編輯] 原來我犯了如下錯誤 docController.delegate =自//代表

回答

3

您需要實現以下的委託方法。代表是從服務提供者到服務消費者的回調,爲即將發生的行爲做好準備。在某些情況下,您必須提供詳細信息(稱爲數據源)才能使用上述功能。

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來閱讀。另外,請看你的具體情況here

+0

嗯仍然得到消息,我不使用任何數據源。關於這個問題的任何文章? – alex

0

對於雨燕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