objective-c - ViewController 不接受我的委托(delegate)

标签 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 方法中设置委托(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/

相关文章:

ios - 无法使用 CustomCell 填充 UITableView

c# - 从回调调用时,应如何为非托管代码返回委托(delegate)?

ios - iOS 应用程序更新是否需要 iPhone 6/6 Plus 支持?

objective-c - 可重用的接口(interface)位,用 IB 设计

ios - 如何制作方形图像的 LazyVGrid?

c# - 将可选参数和命名参数与 Action 和 Func 委托(delegate)一起使用

vb.net - 如何停止/取消处理功能的任务或找到更好的方法来执行此操作?

ios - NSJSONSerialization 崩溃的应用程序

ios - 为什么 AVSampleBufferDisplayLayer 停止显示从 AVCaptureVideoDataOutput 的委托(delegate)中获取的 CMSampleBuffers?

iphone - 为什么在应用程序 :didFinishLaunchingWithOptions: causing an error? 中显示 UIAlertView