为 UIImagePicker returns 错误设置委托

Setting delegate for UIImagePicker returns error

我为 OCR 翻译应用程序编写的某些 swift 代码有问题。 代码片段如下:

@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)
}

image.delegate = self 行 returns 错误:无法将 viewcontroller 类型的值分配给 uiimagepickerdelegate。

我已经在 class 定义中设置了委托,这可以在下面看到...

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

在此先感谢所有帮助。

您在 ViewController class 防御中忘记了 UINavigationControllerDelegate。

图像选择器的委托对象。

声明

unowned(unsafe) var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?

您必须将 UINavigationControllerDelegate 添加到 class 声明中。

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


 // Some thing here

}