为什么UIDocumentMenu代表自己不工作?

时间:2016-05-05 13:49:24

标签: ios swift delegates uidocumentmenuvc

我正在关注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)

   }
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您的委托类应该从视图控制器中的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)

   }
}