我有两节课
A.h类文件
@property (nonatomic, weak) id delegate;
-(void)displayAllGroups:(NSArray *)groups;
A.m类文件
@synthesize delegate;
view didLoad {
_array_groups = [Group getAllGroups:json];
if ([_array_groups isKindOfClass:[NSArray class]])
{
NSLog(@"type of NSArray");//
[delegate displayAllGroups:_array_groups];// error
}
else
{
NSLog(@"not a type of NSArray");
}
}
这是A类的实现,其中我使用id委托将
group_array
发送到b类。 B班吹- (void)viewDidLoad {
[super viewDidLoad];
GroupsViewController *groupView=[[GroupsViewController alloc]init];
[groupView setDelegate:self];
}
-(void)displayAllGroups:(NSArray *)groups
{
NSLog(@"all groups arrays are:%@",groups);
}
注意:错误是
[delegate displayAllGroups:_array_groups];
-[HomeViewController displayAllGroups:]: unrecognized selector sent to instance 0x7fbfcb644b20'
最佳答案
为B类创建协议如下
@protocol B_Delegate <NSObject>
@optional
-(void)displayAllGroups:(NSArray *)groups;
@end
并如下更改委托声明
@property (nonatomic, weak) id <B_Delegate> delegate;
将消除错误。