Swift:类的多重继承错误UIViewController和UIIMagePickerController

时间:2015-02-19 03:09:01

标签: ios swift uiviewcontroller

我手动将另外两个控制器(UINavigationControllerDelegate& UIImagePickerController)添加到UIViewController,我在添加UIImagePickerController后收到错误消息

Multiple inheritance from classes UIViewController and UIImagePickerController

我现在还不确定如何解释和解决这个问题。

由于此错误,我在使用image.delegate方法并将其设置为self

时也会看到一个错误

Type ViewController does not conform to protocol UIImagePickerControllerDelegate

代码:

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerController {

    @IBAction func pickImage(sender: UIButton) {

        var image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false

        self.presentViewController(image, animated: true, completion: nil)


    }

2 个答案:

答案 0 :(得分:25)

您的第一行应该有UIImagePickerControllerDelegate,而不是UIPickerViewController。系统认为你试图让你的控制器继承UIViewController和UIImagePickerController这是不允许的。

答案 1 :(得分:1)

我认为swift并不允许多重继承,协议本质上是其他语言所谓的接口,它们用于相同的目的 - 允许安全且有限的多重继承形式。尝试像这样改变它。详情请见hereprotocol

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate