多个监听器用于委托iOS

我有一个带有代理didSelectString的类搜索栏.我有一个实现委托的A类和一个实现委托的B类.

但是只有来自A类的代理才被执行.代表可以有多个监听器吗?并且如何实现这一点

解决方法

该委托是单一消息传递协议.如果要发送更改的多个对象,则需要使用NSNotifications.

您可以使用通知中心传递对象,如下所示:

NSDictionary *userInfo = @{@"myObject" : customObject};

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:@"myNotificationString" object:self userInfo:userInfo];

想要收听通知

[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(myCustomObserver:)name:@"myNotificationString" object:nil];

并设置选择器

-(void)myCustomObserver:(NSNotification *)notification{
    CustomObject* customObject = notification.userInfo[@"myObject"];
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...