0

我有一个具有以下结构的 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 方法中设置委托,我会收到以下警告:

分配给'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>'不兼容的类型'GCStageViewController *'

有什么想法有什么问题吗?

4

2 回答 2

1

显然 UIImagePicker 需要你也实现 UINavigationControllerDelegate 协议。请参阅文档,其中清楚地表明委托应符合两者。:-)

幸运的是,该协议中的所有方法都是可选的,因此您只需假装:

// change this
@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate> {
// to this
@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
于 2012-07-07T19:42:01.790 回答
1

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

@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> 
于 2012-07-07T19:44:32.730 回答