tags:

views:

728

answers:

4

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?

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?

+1  A: 

You can only have one delegate object. If you want to notify various objects of changes you should use the NSNotificationCenter and post NSNotification messages that your objects can listen for.

Rob Keniger
Actually, this is convention, but not a strict limit. Everyone tends to think only of a `setDelegate:` method, but there is unlimited flexibility.
Quinn Taylor
+1  A: 

You can also have one object handle the event and then forward it to a second object.

vy32
+11  A: 

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#'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
  • Darwin notifications.

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

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

  • notifications with a specific name from a specific object
  • notifications with a specific name from any object
  • notifications from a particular object.

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.

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;

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.

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.

Jeremy W. Sherman
Excellent introduction to notifications and their similarities and differences from delegates.
Rob Napier
Agreed, this is an excellent disambiguation of delegates and notifications (listeners). It's somewhat of a fuzzy line (for example, we often notify delegates about completion of asynchronous tasks) but it pays to understand the distinction.
Quinn Taylor
A: 

It's also important to note that Notifications are a one-way street - They cannot send information back, as delegates can. The only case where you'd want to iterate through a list of delegates is if your delegating class expects back information from its delegates.

Shaun Budhram