我有一个具有以下结构的 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 方法中设置委托(delegate),我会收到以下警告:
Assigning to
'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>'
from incompatible type 'GCStageViewController *'
有什么问题吗?
最佳答案
显然 UIImagePicker 还需要您实现 UINavigationControllerDelegate 协议(protocol)。参见 the documentation ,它清楚地表明委托(delegate)人应该同时符合这两者。 :-)
幸运的是,该协议(protocol)中的所有方法都是可选的,因此您只需假装:
// change this
@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate> {
// to this
@interface GCStageViewController : UIViewController <UIActionSheetDelegate, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
关于objective-c - ViewController 不接受我的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378007/