Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting delegate for UIImagePicker returns error

Having a problem in some swift code I had written for an OCR translation app. The code snippet is below:

@IBAction func btnOCR(sender: AnyObject) {

    var languageAlert = UIAlertController(title: "For Your Information...", message: "The OCR feature currently only supports English & French.", preferredStyle: .Alert)
    languageAlert.addAction(UIAlertAction(title: "Okay", style: .Default, handler: { action in

        var image = UIImagePickerController()
        image.sourceType = UIImagePickerControllerSourceType.Camera
        image.allowsEditing = false
        image.delegate = self
        presentViewController(image, animated: true, completion: nil)

    }))
    self.presentViewController(languageAlert, animated: true, completion: nil)
}

The image.delegate = self line returns the error: Cannot assign a value of type viewcontroller to uiimagepickerdelegate.

I have set the delegate in the class definition, this can be seen below...

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UIImagePickerControllerDelegate {    }

All and any help would be appreciated, thanks in advance.

like image 832
Jacob King Avatar asked Mar 13 '15 14:03

Jacob King


2 Answers

You forgot about UINavigationControllerDelegate in your ViewController class defenition.

The image picker’s delegate object.

Declaration

unowned(unsafe) var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?
like image 192
Pavel Gatilov Avatar answered Nov 12 '22 16:11

Pavel Gatilov


You must add UINavigationControllerDelegate to the class declaration.

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {    


 // Some thing here

}
like image 42
romdav Avatar answered Nov 12 '22 15:11

romdav