为UIImagePicker设置委托会返回错误

时间:2015-03-13 14:35:44

标签: ios swift delegates uiimagepickercontroller

在我为OCR翻译应用程序编写的一些快速代码中遇到问题。 代码段如下:

@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 line返回错误:无法将类型viewcontroller的值赋给uiimagepickerdelegate。

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

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

所有和任何帮助将不胜感激,提前谢谢。

2 个答案:

答案 0 :(得分:81)

您忘记了ViewController类defenition中的UINavigationControllerDelegate。

图像选择器的委托对象。

声明

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

答案 1 :(得分:14)

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

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


 // Some thing here

}