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.

0

2 Answers 2

86

You forgot about UINavigationControllerDelegate in your ViewController class defenition.

The image picker’s delegate object.

Declaration

unowned(unsafe) var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?
Sign up to request clarification or add additional context in comments.

3 Comments

What a stupid mistake! Haha I wont forget again. Thank you very much for your help!
THANK YOU SO MUCH> I DIDNT KNOW I NEEDED UINAVIGATIONCONTROLLERDELEGATE TOO
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
20

You must add UINavigationControllerDelegate to the class declaration.

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


 // Some thing here

}

1 Comment

Thank you for providing an example in swift.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.