从不兼容类型Class __strong分配给“id <delegate>”

时间:2015-06-01 11:06:52

标签: objective-c

在以下代码中,将方法委托给UIView子类 但是,错误Assigning to "id<Delegate>" from imcompatible type Class __strong发生了。

如何修复它以运行-delegateMethod

ViewController.h

@protocol MyDelegate <NSObject>
- (void)delegateMethod;
@end

@interface DetailViewController : UIViewController
@property (nonatomic, assign) id<MyDelegate> delegate;
@end

ViewController.m

- (void)viewDidLoad {
    CustomView *customView = [[CustomView alloc]init];
    self.delegate = customView; //Assigning to "id<Delegate>" from imcompatible type Class __strong
    [self.view addSubview:customView];
}

CustomView.h

@interface CustomView : UIView

CustomView.m

@interface CustomView () <MyDelegate>

- (void)delegateMethod {
    NSLog(@"succeeded");
}

OtherViewController.m

@interface OtherViewController () <MyDelegate>

- (void)aMethod {
    ViewController *vc = [[ViewController alloc]init];
    vc.delegate = self;
}

2 个答案:

答案 0 :(得分:1)

您需要明确指定您的CustomView类符合DetailDelegate协议,仅仅为编译器实现所需的方法是不够的:

@interface CustomView : UIView<DetailDelegate>

答案 1 :(得分:0)

OtherViewController.h 添加/更改

#import "ViewController.h"

@interface CustomView: UIView <DetailDelegate>
相关问题