views:

130

answers:

1

Hi

I'm getting a warning when I set the delegate for UIImagePickerController. It's because UIImagePickerController and its parent UINavigationController both have delegates that can be used. The code works fine but just wondering how to handle delegates and inheritance properly and lose the warning.

So basically I've created my own MyImagePickerController which is subclass UIImagePickerController. UIImagePickerController is a sub class of UINavigationController. so the inheritance tree is

UINavigationController > UIImagePickerController > MyImagePickerController

MyImagePickerController is also the delegate for UIImagePickerController. So I add to my @interface.

@interface MyImagePickerController : UIImagePickerController <UIImagePickerControllerDelegate>...

Then I set MyImagePickerController to be the delegate to itself in loadView

- (void) loadView
{
...
self.delegate = self;
...

} And I implement the UIImagePickerControllerDelegate delegate methods and it all works.

But I get a warning

warning: class 'MyImagePickerController' does not implement the 'UINavigationControllerDelegate' protocol

The problem is that both parent classes have their own Delegates

UINavigationController         > UIImagePickerController         > MyImagePickerController
UINavigationControllerDelegate > UIImagePickerControllerDelegate > MyImagePickerController

And in the definition for UIImagePickerController the delegate method can take either UINavigationControllerDelegate or UIImagePickerControllerDelegate

@interface UIImagePickerController : UINavigationController <NSCoding> {
@property(nonatomic,assign)    id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

The app works, the UIImagePickerControllerDelegate methods in MyImagePickerController are being called but is there a proper way to set the delegate so someone reading the code knows we're trying to implement UIImagePickerControllerDelegate methods and for a newbie to clear the warning.

And what happens if you want to implement both UIImagePickerControllerDelegate and UINavigationControllerDelegate methods in the UIImagePickerControllerDelegate? How would you set the single delegate in Obj-C? Cheers

+1  A: 

Read the warning. It tells you everything you need to know.

warning: class 'MyImagePickerController' does not implement the 'UINavigationControllerDelegate' protocol

This Line:

@property(nonatomic,assign) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

Does not mean that the delegate object (Note delegate object, not delegate method) can be a UINavigationControllerDelegate or a UIImagePickerControllerDelegate. It means that the delegate object must be a UINavigationControllerDelegate and a UIImagePickerControllerDelegate (it must conform to both these protocols).

You seem to be slightly confused about subclassing and inheritance. The problem isn't that both parent classes have their own Delegates. UINavigationController has a delegate property UINavigationControllerDelegate and because UIImagePickerController is a UINavigationController it also has this delegate, which is still UINavigationControllerDelegate, but it has also added extra behaviour, as subclasses tend to do, and the delegate must now not only implement UINavigationControllerDelegate methods but also UIImagePickerControllerDelegate methods.

So the problem isn't "what happens if you want to implement both..". You have to implement both.

mustISignUp