为啥 UIDocumentMenu Delegate to self 不起作用?

Posted

技术标签:

【中文标题】为啥 UIDocumentMenu Delegate to self 不起作用?【英文标题】:Why UIDocumentMenu Delegate to self doesn't work?为什么 UIDocumentMenu Delegate to self 不起作用? 【发布时间】:2016-05-05 13:49:24 【问题描述】:

我正在关注 UIDocumentMenuViewController 的 Apple 文档,以下是我的代码。 importMenu.delegate = self 不起作用并且 Xcode 显示:无法将类型“ViewController”的值分配给类型“UIDocumentMenuDelegate?” .请帮忙。谢谢!

import UIKit

class ViewController: UIViewController  

override func viewDidLoad() 
    super.viewDidLoad()

    let importMenu = UIDocumentMenuViewController(documentTypes: ["public.text", "public.data"], inMode: .Import)

    importMenu.delegate = self

    self.presentViewController(importMenu, animated: true, completion: nil)

   

【问题讨论】:

【参考方案1】:

根据UIDocumentMenuDelegate Protocol Reference,你的ViewController必须符合UIDocumentMenuDelegate并且必须实现documentMenu:didPickDocumentPicker:

extension ViewController: UIDocumentMenuDelegate 
    func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) 
        // do stuffs here
    

【讨论】:

【参考方案2】:

您的委托类应该从视图控制器中的UIDocumentMenuViewDelegate 扩展

import UIKit

class ViewController: UIViewController, UIDocumentMenuViewDelegate  

override func viewDidLoad() 
    super.viewDidLoad()

    let importMenu = UIDocumentMenuViewController(documentTypes: ["public.text", "public.data"], inMode: .Import)

    importMenu.delegate = self

    self.presentViewController(importMenu, animated: true, completion: nil)

   

【讨论】:

嗨 Özgür Ersil,根据 document,delegate 的类型是 UIDocumentMenuDelegate 而不是 UIDocumentMenuViewControllerDelegate。据我所知,UIKit 中没有名为 UIDocumentMenuViewControllerDelegate 的东西

以上是关于为啥 UIDocumentMenu Delegate to self 不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

使用NSURLSessionDownloadTask实现大文件下载-监听下载进度

使用 ARC 声明委托属性的推荐方法

在 IOS 9 及更低版本中创建 nsmanagedcontext

关于C#委托应用与总结

QML 中神秘的 Component

facebook-ios-sdk 注销问题