Swift:来自 类 错误 UIViewController 和 UIIMagePickerController 的多重继承
Swift: Multiple Inheritance from classes Error UIViewController and UIIMagePickerController
我手动添加了两个额外的控制器(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)
}
我认为 swift 不允许多重继承,协议本质上是其他语言所称的接口,它们服务于相同的目的 - 允许安全和有限形式的多重继承。试着像这样改变它。查看来自 here and protocol.
的更多信息
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate
你的第一行应该有 UIImagePickerControllerDelegate,而不是 UIPickerViewController。系统认为您试图让您的控制器继承 UIViewController 和 UIImagePickerController,这是不允许的。
我手动添加了两个额外的控制器(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)
}
我认为 swift 不允许多重继承,协议本质上是其他语言所称的接口,它们服务于相同的目的 - 允许安全和有限形式的多重继承。试着像这样改变它。查看来自 here and protocol.
的更多信息class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate
你的第一行应该有 UIImagePickerControllerDelegate,而不是 UIPickerViewController。系统认为您试图让您的控制器继承 UIViewController 和 UIImagePickerController,这是不允许的。