我有一个带有委托(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/