Cazforshort
Cazforshort

Reputation: 105

Xcode Document Picker Delegate in UIKit

Why cant I set the delegate to self? I'm running the code below in the viewcontroller.swift file, but I want to call it in a Render.swift file. I've got a metal scene running of the camera view and want to open the file picker in front of it, but it doesn't seem to be very simple.

@IBAction func importFiles(_ sender: Any) {
    
    let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePlainText as String], in: .import)
    documentPicker.delegate = self
    documentPicker.allowsMultipleSelection = false
    present(documentPicker, animated: true, completion: nil)
}

It says Cannot assign value of type 'ViewController' to type 'UIDocumentPickerDelegate

In my Renders.swift file I have a function tied to a button that calls the following:

 //open dialog for picker...
    let myVC: ViewController = ViewController()
    
    myVC.importFiles()

I'm pretty new to this all.

Upvotes: 1

Views: 446

Answers (1)

matt
matt

Reputation: 535067

Why cant I set the delegate to self?

Because the delegate needs to be a UIDocumentPickerDelegate, and self is not a UIDocumentPickerDelegate. You need to declare that it is:

class ViewController : UIViewController, UIDocumentPickerDelegate {

That may precipitate other issues (and I expect it will), but at least you'll get past the point where you are now.

Upvotes: 3

Related Questions