目标C中的多个代表 [英] Multiple Delegates in Objective C

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

问题描述

我来自C#事件模型,我想知道是否有一种标准的方法可以将事件通知多个委托人?

I'm coming from the C# event model, and I'm wondering if there is a standard way to notify multiple delegates of an event?

我有一个ClassCDelegate协议,我想同时实现ClassA和ClassB.有没有一种方法可以将ClassA和ClassB的ClassC实例都分配为委托,而不必在ClassC中手动创建委托变量列表并对其进行迭代?

I have a ClassCDelegate protocol that I want both ClassA and ClassB to implement. Is there a way I can assign an instance of ClassC both ClassA and ClassB as delegates without having to manually create a list of delegate variables within ClassC and iterate through them?

推荐答案

可可委托用于完成控制的反转并减少子类化的需要.单个对象可以有多个委托,但这是在将不同类型的决策委托给不同对象的情况下完成的.一个很好的例子是WebKit的WebView,它有五个代表,负责资源加载和导航策略等各个领域.

Cocoa delegates are used to accomplish inversion of control and lessen the need for subclassing. It's entirely possible to have multiple delegates for a single object, but this is done when it makes sense to delegate different kinds of decisions to different objects. A great example of this is WebView from WebKit, which has five delegates responsible for areas as diverse as resource loading and navigation policy.

C#的事件委托系统最接近Cocoa提供的几种通知API,该系统允许一个对象向另一个对象注册,以便在发生特定事件时得到通知.您可能会运行的各种API,从最高级别到最低级别:

C#'s event–delegate system, which allows an object to register with another object to be notified when a particular event occurs, is closest to the several notification APIs available from Cocoa. The various APIs you might run across are, from highest level to lowest:

  • NSNotificationCenter
  • NSDistributedNotificationCenter
  • CFNotificationCenter
  • 达尔文通知.
  • NSNotificationCenter
  • NSDistributedNotificationCenter
  • CFNotificationCenter
  • Darwin notifications.

所有内容在精神上都是相似的,因此在这种情况下,我将只考虑您要使用的一个:NSNotificationCenter.

All are similar in spirit, so I'll only consider the one you'd use in this case: NSNotificationCenter.

观察者(例如ClassA和ClassB)通过NSNotificationCenter注册他们对通知的兴趣.他们可以指定对

Observers, such as ClassA and ClassB, register their interest in notifications with NSNotificationCenter. They can specify an interest in

  • 来自特定对象的具有特定名称的通知
  • 来自任何对象的具有特定名称的通知
  • 来自特定对象的通知.

将匹配的通知发布到通知中心时,通过在注册时向通知中心调用他们提供的方法来通知观察者.该方法始终具有相同的类型:它不返回任何内容,并接受单个参数NSNotification对象.

When a matching notification is posted to the notification center, observers are notified by invoking the method they supplied at registration time with the notification center. The method always has the same type: it returns nothing and accepts a single argument, an NSNotification object.

通常,您可以通过让ClassC在其头文件中为通知名称声明一个常量来处理您的情况,

You would generally handle your situation by having ClassC declare a constant for the notification name in its header file, for example,

extern NSString *const ClassCSomethingDidHappenNotification;

感兴趣的观察者,例如ClassA和ClassB,可以在此通知中注册兴趣:

Interested observers, such as ClassA and ClassB, can then register interest in this notification:

[[NSNotificationCenter defaultCenter]
  addObserver:self
     selector:@selector(handleSomethingDidHappen:)
         name:ClassCSomethingDidHappenNotification                
       object:aClassCObject];

现在您不必注册选择器并向观察者添加方法来处理回调,现在还可以注册操作队列和在发布匹配通知时在该队列上执行的块.

Instead of registering a selector and adding a method to the observer to handle the callback, you can also now register an operation queue and a block to execute on that queue when a matching notification is posted.

与通知关联的事件发生时,ClassC将通知发布到通知中心:

When the event associated with the notification occurs, ClassC posts the notification to the notification center:

[[NSNotificationCenter defaultCenter]
  postNotificationName:ClassCSomethingDidHappenNotification
                object:self];

然后,通知中心将浏览观察者列表,找到与该通知匹配的观察者,并调用适当的方法.

The notification center will then look through the list of observers, find those that match this notification, and invoke the appropriate method.

这篇关于目标C中的多个代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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