ios - id delegate unrecognized selector 发送到实例

标签 ios objective-c delegates unrecognized-selector

我有两个类(class)

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 委托(delegate)将 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:]: 无法识别的选择器发送到实例 0x7fbfcb644b20'

最佳答案

如下所示为 B 类创建协议(protocol)

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

并如下更改委托(delegate)声明

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

将消除错误。

关于ios - id delegate unrecognized selector 发送到实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34554475/

相关文章:

objective-c - 电影等大文件的 iOS 本地化

objective-c - NSString(或字符串)的长度是否影响 isEqualToString 的性能 : (or ==)?

C# 以可变参数列表作为参数传递函数

ios - 如何从另一个 ViewController 引用现有数据库

ios - 为 UIAlertView 创建摇动动画

ios - 更改 Apple Watch 标签内的文本对齐方式

c# - 如何基于包含其名称的字符串执行方法

iOS:UIApplication DidReceiveRemoteNotification

ios - addSubView当前uiview

ios - UITableViewCell 单元重用和 UILabel 自动布局动态调整大小