patrickS
patrickS

Reputation: 3760

ViewController doesn't accept my delegate

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?

Upvotes: 0

Views: 330

Answers (2)

CodaFi
CodaFi

Reputation: 43330

The UIImagePickerControllerDelegate actually requires conformation to two protocols: UIImagePickerControllerDelegate and UINavigationControllerDelegate. Conform to UINavigationControllerDelegate in the header and the error will disappear.

@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> 

Upvotes: 1

Chris Trahey
Chris Trahey

Reputation: 18290

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:

// change this
@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate> {
// to this
@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {

Upvotes: 1

Related Questions