ios Swift:来自类 Error UIViewController 和 UIIMagePickerController 的多重继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28598014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:53:26  来源:igfitidea点击:

Swift: Multiple Inheritance from classes Error UIViewController and UIIMagePickerController

iosswiftuiviewcontroller

提问by cphill

I manually added two additional controllers (UINavigationControllerDelegate& UIImagePickerController) to a UIViewControllerand I am receiving an error after adding the UIImagePickerControllerthat says,

我手动添加了两个额外的控制器 ( UINavigationControllerDelegate& UIImagePickerController) 到 aUIViewController并且在添加后我收到一个错误UIImagePickerController,上面写着,

Multiple inheritance from classes UIViewController and UIImagePickerController

Multiple inheritance from classes UIViewController and UIImagePickerController

I'm not sure how to interpret and fix that right now.

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

As a result of this error, I'm also seeing one when I use the image.delegatemethod and set it equal to self

由于此错误,当我使用该image.delegate方法并将其设置为等于时,我也看到了一个错误self

Type ViewController does not conform to protocol UIImagePickerControllerDelegate

Type ViewController does not conform to protocol UIImagePickerControllerDelegate

Code:

代码:

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)


    }

回答by rdelmar

You should have UIImagePickerControllerDelegate, not UIPickerViewController in your first line. The system thinks that you are trying to have your controller inherit from both UIViewController and UIImagePickerController which is not allowed.

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

回答by gabbler

I think it is that swift doesn't allow multiple inheritance, protocols are essentially what other languages call interfaces and they serve the same purpose - to allow a safe and limited form of multiple inheritance. Try to change it like this. See more from hereand protocol.

我认为 swift 不允许多重继承,协议本质上是其他语言所称的接口,它们服务于相同的目的 - 允许安全和有限形式的多重继承。试着像这样改变它。从这里协议中查看更多信息

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate