【问题标题】:Swift: Multiple Inheritance from classes Error UIViewController and UIIMagePickerControllerSwift:来自类错误 UIViewController 和 UIIMagePickerController 的多重继承
【发布时间】:2015-02-19 03:09:01
【问题描述】:

我手动向UIViewController 添加了两个额外的控制器(UINavigationControllerDelegateUIImagePickerController),在添加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)


    }

【问题讨论】:

    标签: ios swift uiviewcontroller


    【解决方案1】:

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

    【讨论】:

    • 该死的自动补全! :)
    【解决方案2】:

    我认为 swift 不允许多重继承,协议本质上是其他语言所称的接口,它们具有相同的目的 - 允许安全且有限的多重继承形式。试着像这样改变它。查看更多来自hereprotocol

    class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-14
      • 2014-12-27
      • 1970-01-01
      • 2019-04-13
      • 2018-01-19
      • 2012-02-15
      • 2023-03-19
      • 2012-11-16
      相关资源
      最近更新 更多