Setting the UIImagePickerController delegate throws a warning about UINavigationControllerDelegate
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 UIImagePickerContr开发者_开发问答oller care if my class implements the UINavigationControllerDelegate protocol?
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.
You can do it like this at the .h file
@interface MyController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
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.
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
for Swift 4 :
@interface MyController : UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate
精彩评论