【问题标题】:Assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from incompatible type 'ViewController *const__strong'从不兼容类型 'ViewController *const__strong' 分配给 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>'
【发布时间】:2014-12-13 03:17:09
【问题描述】:

我的应用程序中有一个 ImagePickerController。 效果很好,但是在ipc.delegate = self;旁边出现错误信息:

分配给 'ID' 来自不兼容的类型'ViewController *const__strong'

该应用运行良好,因此我忽略了错误消息,但我想我需要知道原因。为什么会出现错误信息?

ipc = [[UIImagePickerController alloc]init];
            ipc.modalPresentationStyle = UIModalPresentationCurrentContext;
            ipc.delegate = self;
            ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [ipc setAllowsEditing:NO];
            [self presentViewController:ipc animated:NO completion:nil];

【问题讨论】:

    标签: ios xcode delegates uiimagepickercontroller incompatibletypeerror


    【解决方案1】:

    如果您查看UIImagePickerController 委托属性的定义,您会看到它定义为:

    @property(nonatomic, assign) id<UINavigationControllerDelegate, 
                                    UIImagePickerControllerDelegate> delegate 
    

    您设置为委托的任何对象(在本例中您使用的是self)都必须符合UINavigationControllerDelegate 协议和UIImagePickerControllerDelegate 协议。如果对象不符合这两个协议,您将收到编译时警告。

    以下是您声明您的类符合协议的方式:

    @interface MyViewController : UIViewController <UINavigationControllerDelegate, 
                                                    UIImagePickerControllerDelegate>
    

    阅读working with protocolsUINavigationControllerDelegateUIImagePickerControllerDelegate

    【讨论】:

    • 我已经将它们添加到“ViewController.h”文件中。这就是为什么我不知道为什么会出现错误。
    • @홍승욱 确保你声明你的类符合协议。 @interface MyViewController : UIViewController &lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate&gt;
    【解决方案2】:

    我只是第一次遇到这种情况。如果您的类扩展了另一个符合其他协议的类,并且您的类也符合这两个协议&lt;UINavigationControllerDelegate, UIImagePickerControllerDelegate&gt;,那么为了消除警告,您必须将其强制转换,如下所示:

    ipc.delegate = (id<<UINavigationControllerDelegate, UIImagePickerControllerDelegate>) self;
    

    就个人而言,我认为这是编译器中的一个错误,因为您确实遵守了这两种协议,但您也恰好遵守了其他协议。因此,您不必看到警告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多