我已将 UIImagePickerController
添加到 UIViewController
。我还将 UIImagePickerControllerDelegate 分配给了 UIViewController。
当我执行以下行时,
myPicker.delegate = self;
Xcode 向我发送以下消息:
warning: assigning to id from incompatible type 'RootViewController'
然后我将 UINavigationControllerDelegate
协议(protocol)添加到同一个 UIViewController
中,错误消息消失了。
那么,当我添加 UIImagePickerController
时,我是否必须将这两个协议(protocol)添加到 UIViewController
中?
如果 UIImagePickerController
是 UINavigationController
的子类,如文档中所述,这不应该是自动的吗?为什么我必须添加其父级的委托(delegate)协议(protocol)而不仅仅是 UIImagePickerControllerDelegate
协议(protocol)?
这是一个错误还是我遗漏了什么?
最佳答案
正如您所指出的,UIImagePickerController
继承自 UINavigationController
。但它使用相同的 delegate
属性,并且没有声明自己的(假设的)“imagePickerDelegate”,因此您的委托(delegate)必须符合这两个协议(protocol)。这是有道理的,因为您还将相同的委托(delegate)分配给 UINavigationController
部分(它对图像选择器一无所知)。
在我看来,API 设计有点问题,但无论如何,UINavigationControllerDelegate
中的所有方法都是可选的,因此只需声明您符合协议(protocol)并完成它即可。
关于iPhone - UIImagePickerControllerDelegate 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4727895/