代表iOS的多个侦听器 [英] multiple listeners for delegate iOS

查看:53
本文介绍了代表iOS的多个侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class search bar with a delegate didSelectString. I have an class A that implements the delegate and a class B that implements the delegate.

但是,仅执行来自类A的委托.代表可以有多个侦听器吗?以及我该如何实现

However only the delegate from class A is executed. Can a delegate have multiple listeners? and how do I implement this

推荐答案

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

The delegation is a single messaging protocol. You'll need to use NSNotifications if you want to message multiple objects of a change.

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

You can pass an object using notifications centre like so:

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的多个侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆