开发者

iPhone - UIImagePickerControllerDelegate inheritance

开发者 https://www.devze.com 2023-02-05 03:33 出处:网络
I have added a UIImagePickerController to a UIViewController. I have also assigned the UIImagePickerControllerDelegate to that UIViewController.

I have added a UIImagePickerController to a UIViewController. I have also assigned the UIImagePickerControllerDelegate to that UIViewController.

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 same UIViewController and the error message vanished.

So, do I have to add both protocols to the UIViewController when I add a UIImagePickerController?

If the UIImagePickerController is a subclass of UINaviga开发者_StackOverflow社区tionController as stated in the docs, shouldn't this be automatic? Why do I have to add its parent's delegate protocol and not just the UIImagePickerControllerDelegate protocol?

Is this a bug or am I missing something?


As you noted, UIImagePickerController inherits from UINavigationController. It uses the same delegate 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 the UINavigationController 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.


Add these code like below,you can see the warning disappear.

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

The Protocol of UIImagePickerController and UINavigationController must be added in your interface, this can make the warning invisible.

0

精彩评论

暂无评论...
验证码 换一张
取 消