【问题标题】:UIImagePickerController Delegate Warning - Incompatible typeUIImagePickerController 委托警告 - 不兼容的类型
【发布时间】:2023-03-19 20:54:02
【问题描述】:

我正在尝试链接我的照片库以在 UIImageView 中显示图像,这将允许我添加过滤器,但是当您选择照片并点击“选择”时,应用程序崩溃。下面是我正在使用的代码; - (IBAction)chooseImage:(id)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.allowsEditing = YES;

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:nil];

}

错误在以下行:

"picker.delegate = self;" 

并记为 *"Assigning to 'id' from in compatible type 'VSViewController const_strong'

如果有人对如何修复它或将运行的替代代码提出建议,我们将不胜感激。

请尽量避免使用过于复杂的语言,我是编码新手,这个问题让我沮丧了一段时间。

【问题讨论】:

  • 您是否已将 UIImagePickerController 委托包含到您的 .h 中?
  • 我不这么认为,那是什么编码?
  • 在 VSAppDelegate.h 还是 VSViewController.h 下?我都试过了,但都没有'@protocol'
  • @JMarsh 很可能是指@interface,而不是@protocol

标签: ios objective-c xcode5 appdelegate photolibrary


【解决方案1】:

您的课程实际上需要同时符合UIImagePickerControllerDelegateUINavigationControllerDelegate。这是在类声明中完成的:

@interface VSViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
/* ... */
@end

这里已经说明了原因:iPhone - UIImagePickerControllerDelegate inheritance

【讨论】:

  • 在项目中的什么地方输入?代表或 .h 或 .m 文件?我需要在主文件中添加任何内容吗?
  • VSViewController 是你写的类吗?
  • 你的意思是它是我正在编码的那个吗?
  • 我的假设是VSViewController是你自己创建的一个类,并且它有对应的头文件和实现文件,比如VSViewController.hVSViewController.m。对吗?
  • 我还从您给我们的错误以及您提供的代码 sn-p 中假设,您实际上是在 VSViewController 的类方法中编码。
【解决方案2】:

在你的头文件中,你需要遵守协议。

@interface YourViewController : UIViewController&lt;UIImagePickerControllerDelegate&gt;

然后你需要实现协议选择器。

阅读此内容以供参考:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html

【讨论】:

  • 确定一下,哪个是头文件?
  • 头文件是以.h 结尾的那个,@David Xu,你需要更新你的代码。 UIImagePickerControllerDelegate 也需要符合 UINavigationController 协议。
猜你喜欢
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2015-10-10
  • 2011-04-14
相关资源
最近更新 更多