39

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.

Richard Slater
  • 6,313
  • 4
  • 53
  • 81
Jacob King
  • 6,025
  • 4
  • 27
  • 45

2 Answers2

86

You forgot about UINavigationControllerDelegate in your ViewController class defenition.

The image picker’s delegate object.

Declaration

unowned(unsafe) var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?
Pavel Gatilov
  • 2,570
  • 2
  • 18
  • 31
  • What a stupid mistake! Haha I wont forget again. Thank you very much for your help! – Jacob King Mar 13 '15 at 15:01
  • 1
    THANK YOU SO MUCH> I DIDNT KNOW I NEEDED UINAVIGATIONCONTROLLERDELEGATE TOO – coolcool1994 Jul 29 '16 at 20:06
  • Newbie to IOS. If I may ask. In the UI controller even if definition mentions about UINavigationControllerDelegate. But without any specific implementation, only by declaring UINavigationControllerDelegate does it really help? How compiler takes into consideration without implementing anything related to UINavigationControllerDelegate. I am totally new to IOS swift. Would appreciate if someone could explain – Sreehari Mar 04 '19 at 16:59
20

You must add UINavigationControllerDelegate to the class declaration.

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


 // Some thing here

}
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
romdav
  • 241
  • 2
  • 4