ViewController不接受我的委托

时间:2012-07-07 19:26:20

标签: objective-c ios delegates

我有一个具有以下结构的ViewController类:

标题文件:

@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

实施档案:

- (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];
}

如果我在takePicture方法中设置委托,我会收到以下警告:

  

从不兼容类型'GCStageViewController *'

分配给'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>'

任何想法有什么不对?

2 个答案:

答案 0 :(得分:1)

显然UIImagePicker还需要您实现UINavigationControllerDelegate协议。请参阅the documentation,其中清楚地显示委托应符合两者。 : - )

幸运的是,该协议中的所有方法都是可选的,所以你需要做的就是假装:

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

答案 1 :(得分:1)

UIImagePickerControllerDelegate实际上需要符合两个协议:UIImagePickerControllerDelegateUINavigationControllerDelegate。符合标题中的UINavigationControllerDelegate,错误将消失。

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