简体   繁体   English

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

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

[new to swift] I testing this function to export some simple file [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
    }

But when i clicked the export button i am getting the message 但是当我点击导出按钮时,我收到了消息

UIDocumentInteractionController delegate must implement documentInteractionControllerViewControllerForPreview: to allow preview

I thought 我想

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate { class ViewController:UIViewController,UIDocumentInteractionControllerDelegate {

Would be sufficient? 会足够吗?

I tried Self.documentInteractionControllerViewForPreview(docController) 我试过Self.documentInteractionControllerViewForPreview(docController)

[edit] turned out i had made the following mistake docController.delegate = self//delegate [编辑]原来我犯了以下错误docController.delegate = self //委托

You need to implement following delegate methods. 您需要实现以下委托方法。 Delegates are the callbacks from the service provider to service consumer to be prepared for the action that is about to occur. 代表是从服务提供者到服务使用者的回调,为即将发生的操作做好准备。 On some occasions you must provide details (called data source) in order to utilise the said functionality. 在某些情况下,您必须提供详细信息(称为数据源)才能使用所述功能。

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

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

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

Read through how delegation works . 阅读委托如何运作 Also, take a look at your specific case here . 另外,请在此处查看您的具体案例。

For Swift 3.0 对于Swift 3.0

First extend your class 首先扩展你的课程

UIDocumentInteractionControllerDelegate

Then only implement the following method 然后只实现以下方法

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

    return self
}

Calling the Pdf Viewer 调用Pdf Viewer

 func MyViewDocumentsmethod(){

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



}

This should render the following 这应该呈现以下内容

在此输入图像描述

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

相关问题 如何通过Swift扩展实现委托方法 - How do I implement delegate methods through Swift extension 如何在Swift中实现UIPageViewController委托的transitionCompleted? - How do I implement transitionCompleted of the UIPageViewController delegate in Swift? 尝试实现委托继承 - Trying to Implement Delegate Inheritance 我如何从 swift 编写的 iOS 中准确导出 csv 文件? - How do i exactly export a csv file from iOS written in swift? 如何将数据从应用程序委托传输到另一个类 - How do I transfer data from the app delegate to another class 如何从UITableViewDataSource设置UITableViewCell的委托? - How do I set the delegate of a UITableViewCell from a UITableViewDataSource? 如何从 App Delegate 切换到不同的视图 controller? - How do I switch to a different view controller from the App Delegate? 如何从应用程序委托打开视图 controller? - How do I open a view controller from the app delegate? 如何以正确的方式实现委托方法? - How I can implement delegate method in the correct way? 如何为从应用程序的不同位置创建的 object 实现委托 - How to implement a delegate for an object that gets created from different places in the app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM