iPhone - UIImagePickerControllerDelegate 继承

标签 iphone inheritance uinavigationcontroller delegates uiimagepickercontroller

我已将 UIImagePickerController 添加到 UIViewController。我还将 UIImagePickerControllerDelegate 分配给了 UIViewController。

当我执行以下行时,

myPicker.delegate = self;

Xcode 向我发送以下消息:

warning: assigning to id from incompatible type 'RootViewController'

然后我将 UINavigationControllerDelegate 协议(protocol)添加到同一个 UIViewController 中,错误消息消失了。

那么,当我添加 UIImagePickerController 时,我是否必须将这两个协议(protocol)添加到 UIViewController 中?

如果 UIImagePickerControllerUINavigationController 的子类,如文档中所述,这不应该是自动的吗?为什么我必须添加其父级的委托(delegate)协议(protocol)而不仅仅是 UIImagePickerControllerDelegate 协议(protocol)?

这是一个错误还是我遗漏了什么?

最佳答案

正如您所指出的,UIImagePickerController 继承自 UINavigationController。但它使用相同的 delegate 属性,并且没有声明自己的(假设的)“imagePickerDelegate”,因此您的委托(delegate)必须符合这两个协议(protocol)。这是有道理的,因为您还将相同的委托(delegate)分配给 UINavigationController 部分(它对图像选择器一无所知)。

在我看来,API 设计有点问题,但无论如何,UINavigationControllerDelegate 中的所有方法都是可选的,因此只需声明您符合协议(protocol)并完成它即可。

关于iPhone - UIImagePickerControllerDelegate 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4727895/

相关文章:

ruby - 在父方法中获取子常量 - Ruby

java - 有没有办法在 Java 中全局处理异常?

iOS 13 - 大标题中的变音符号导致内容跳跃和警告 UITableViewAlertForLayoutOutsideViewHierarchy

iphone - 如何在 Cocoa Touch 中对字符串进行 base64url 编码

iphone - dealloc 中的自定义 UIButton 内存管理

ios - RestKit - 如果嵌套值是域名,则嵌套对象映射失败

c++ - 私有(private)继承和构造函数

swift - 通过 UITableViewController 模态呈现 UIPageViewController 时导航栏和工具栏上的图形制品

iphone - 我可以弹出到特定的 ViewController 吗?

iphone - 页脚背景未在 iPhone 上显示