23

The code:

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.navigationController presentModalViewController:picker animated:YES];
[picker release];

The warning, which shows up on the picker.delegate = self; line:

Class 'CardEditor' does not implement the 'UINavigationControllerDelegate' protocol

Why does the UIImagePickerController care if my class implements the UINavigationControllerDelegate protocol?

5 Answers 5

44

UIImagePickerController inherits from UINavigationController. The delegate property is part of UINavigationController, so it requires that its delegate conforms to the UINavigationControllerDelegate protocol.

The delegate property is declared like so:

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

This tells the compiler to verify that your controller will implement the UINavigationConrollerDelegate protocol. The check is in place so that a compile-time warning can be generated if your controller class does not properly implement all of the methods that the UINavigationController might send to its delegate.

If you're looking for a solution, you can indicate (in your controller class interface) that your controller is compliant with that protocol:

@interface MyController : NSObject <UINavigationControllerDelegate>
    ...
@end

Once this is done, the compiler will warn you about any required delegate methods that you haven't implemented. This is a good thing, as it will prevent you from releasing code that might accidentally throw a "method not found" runtime error.

3
  • Thanks for this, I just came across this error and you provided a quick solution :)
    – KomodoDave
    Oct 12, 2011 at 14:30
  • @e.James interface MyController : NSObject <UIImagePickerControllerDelegate,UINavigationControllerDelegate> ...
    – yunas
    May 28, 2012 at 7:52
  • Best anser I found for this problem. Mar 25, 2013 at 5:57
16

You can do it like this at the .h file

@interface MyController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
6

e.James is right. Besides this UINavigationControllerDelegate only specifies optional methods. So a quick solution is to just let your view controller implement the protocol without implementing any of its methods.

I've checked all the Apple Sample code for implementations of UIImagePickerControllerDelegate and they all don't implement any of the UINavigationControllerDelegate methods. So if you don't need them don't worry.

3

I also faced the same problem then after a search found the solution i.e to add UINavigationControllerDelegate along with your UIImagePickerControllerDelegate in .h file.

@interface LatestJobsVC : UIViewController <UIImagePickerControllerDelegate , UINavigationControllerDelegate>

Hope that helps you. Thanks

0

for Swift 4 :

@interface MyController : UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.