id委托无法识别的选择器发送到实例

时间:2016-01-01 07:48:22

标签: ios objective-c delegates unrecognized-selector

我有两个班级

班级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'

1 个答案:

答案 0 :(得分:1)

为B类创建协议,如下所示

@protocol B_Delegate <NSObject>
@optional
-(void)displayAllGroups:(NSArray *)groups;
@end

更改委托声明如下

@property (nonatomic, weak) id <B_Delegate> delegate;

将删除错误。