iPhone - UIImagePickerControllerDelegate继承 [英] iPhone - UIImagePickerControllerDelegate inheritance
问题描述
我已经向 UIViewController
添加了一个 UIImagePickerController
。我还将 UIImagePickerControllerDelegate
分配给 UIViewController
。
当我执行以下行时,
myPicker.delegate =自;
Xcode 给我发送以下消息:
警告:从不兼容类型RootViewController分配给
id
$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ p $ U $ >并且错误消息消失。
所以,当我添加时,我必须将两个协议添加到
UIViewController
一个UIImagePickerController
?
如果
UIImagePickerController
是一个子类的文件中所述的UINavigationController
不应该是自动的?为什么我必须添加其父代理协议,而不只是添加UIImagePickerControllerDelegate
协议?
这是一个错误或我没有发现什么?
解决方案正如你所说,
UIImagePickerController
UINavigationController
。它使用相同的委托
属性,并且不声明自己的(假设的)imagePickerDelegate,因此您的委托必须符合这两个协议。这是有道理的,因为您也将相同的代理分配给UINavigationController
部分(对于图像选择器一无所知)。
在我看来,API设计有点可疑,但无论如何,
UINavigationControllerDelegate
中的所有方法都是可选的,因此可以声明您符合协议,并完成它。I have added a
UIImagePickerController
to aUIViewController
. I have also assigned theUIImagePickerControllerDelegate
to thatUIViewController
.When I execute the following line,
myPicker.delegate = self;
Xcode gifts me with the following message:
warning: assigning to id from incompatible type 'RootViewController'
Then I added the
UINavigationControllerDelegate
protocol to the sameUIViewController
and the error message vanished.So, do I have to add both protocols to the
UIViewController
when I add aUIImagePickerController
?If the
UIImagePickerController
is a subclass ofUINavigationController
as stated in the docs, shouldn't this be automatic? Why do I have to add its parent's delegate protocol and not just theUIImagePickerControllerDelegate
protocol?Is this a bug or am I missing something?
解决方案As you noted,
UIImagePickerController
inherits fromUINavigationController
. It uses the samedelegate
property though and doesn't declare a (hypothetical) "imagePickerDelegate" of its own, so your delegate has to conform to both protocols. It makes sense, because you're also assigning the same delegate to theUINavigationController
part (that knows nothing about the image picker).The API design is a bit questionable here in my opinion, but anyway, all methods in
UINavigationControllerDelegate
are optional, so it suffices to declare that you conform to the protocol and be done with it.这篇关于iPhone - UIImagePickerControllerDelegate继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!