在以下代码中,将方法委托给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;
}
答案 0 :(得分:1)
您需要明确指定您的CustomView
类符合DetailDelegate
协议,仅仅为编译器实现所需的方法是不够的:
@interface CustomView : UIView<DetailDelegate>
答案 1 :(得分:0)
在 OtherViewController.h 添加/更改
#import "ViewController.h"
@interface CustomView: UIView <DetailDelegate>