Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the UIImagePickerController delegate throws a warning about UINavigationControllerDelegate

Tags:

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?

like image 699
synic Avatar asked May 13 '10 19:05

synic


4 Answers

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.

like image 175
e.James Avatar answered Nov 15 '22 14:11

e.James


You can do it like this at the .h file

@interface MyController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
like image 32
Bin Avatar answered Nov 15 '22 14:11

Bin


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.

like image 21
stigi Avatar answered Nov 15 '22 13:11

stigi


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

like image 26
Kunal Gupta Avatar answered Nov 15 '22 13:11

Kunal Gupta