iPhone - UIImagePickerControllerDelegate继承

时间:2011-01-18 18:58:23

标签: iphone inheritance uinavigationcontroller delegates uiimagepickercontroller

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

当我执行以下行时,

myPicker.delegate = self;

Xcode 赠送给我以下消息:

  

警告:分配给   ID   来自不兼容的类型'RootViewController'

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

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

如果UIImagePickerController是文档中所述的UINavigationController的子类,那么这不应该是自动的吗?为什么我必须添加其父代理协议而不仅仅是UIImagePickerControllerDelegate协议?

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

2 个答案:

答案 0 :(得分:61)

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

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

答案 1 :(得分:33)

如下所示添加这些代码,您可以看到警告消失。

@interface viewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> { }
@end

必须在您的界面中添加UIImagePickerControllerUINavigationController的协议,这可能会使警告不可见。

相关问题