Why UIDocumentMenu Delegate to self doesn't work?

676 views Asked by At

I am following Apple documentation for UIDocumentMenuViewController and the following is my code. importMenu.delegate = self doesn't work and Xcode shows: Cannot assign value of type 'ViewController' to type 'UIDocumentMenuDelegate?' . Please help. Thanks!

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

There are 2 answers

0
Cheng-Yu Hsu On BEST ANSWER

According to UIDocumentMenuDelegate Protocol Reference, your ViewController must conforms to UIDocumentMenuDelegate and must implements documentMenu:didPickDocumentPicker:

extension ViewController: UIDocumentMenuDelegate {
    func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        // do stuffs here
    }
}
1
Özgür Ersil On

your delegation class should extend from UIDocumentMenuViewDelegate in the view controller

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)

   }
}