I have a ViewController class with the following structure:
Header file:
@protocol GCStageViewControllerDelegate;
@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate> {
id <GCStageViewControllerDelegate> delegate;
...
}
@property (nonatomic, retain) id <GCStageViewControllerDelegate> delegate;
...
@end
@protocol GCStageViewControllerDelegate
- (void)gcStageViewContollerDidFinish:(GCStageViewController *)controller withGCStageItem:(GCStageItem *)item;
@end
Implementation file:
- (void)viewDidLoad {
...
stageInputTextField.delegate = self; // works
...
}
- (void)takePicture {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
imagePicker.delegate = self; // here I get the error
[self presentModalViewController:imagePicker animated:YES];
}
If I set the delegate in the takePicture method I get the following warning:
Assigning to
'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>'from incompatible type ‘GCStageViewController *’
Any ideas what’s wrong?
Apparently UIImagePicker needs you to also implement the UINavigationControllerDelegate protocol. See the documentation, where it clearly shows that the delegate should conform to both. 🙂
Luckily, all of the methods in that protocol are optional, so all you need to do it pretend: