ios - 委托(delegate) iOS 的多个监听器

标签 ios

我有一个带有委托(delegate) didSelectString 的类搜索栏。我有一个实现委托(delegate)的 A 类和一个实现委托(delegate)的 B 类。

但是只有来自类 A 的委托(delegate)被执行。一个代表可以有多个听众吗?以及如何实现这个

最佳答案

委托(delegate)是一个单一的消息传递协议(protocol)。如果您想向多个对象发送更改消息,则需要使用 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 - 委托(delegate) iOS 的多个监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19464958/

相关文章:

ios - Interface Builder 中的 "User Defined Runtime Attributes"是什么?

ios - 如何快速制作不同形状的 ImageView

ios - UINavigationBar 上方的标题

iphone - 获取 -[__NSArrayI addObjectsFromArray :]: unrecognized selector sent to instance 0xa0c5f70 error

ios - 需要 wantsFullScreenLayout 行为与 iOS 7 上的非半透明栏

ios - 快速获取字典中选定键的值

android - 如何使用 sqlite 查询获取当前月份或指定月份的日期?

ios - 每当 UITextView 更改时,如何从空的 attributedString 开始?

ios - 我的应用已被接受,但出现 "Missing Push Notification Entitlement"警告

javascript - react native 自定义组件