Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Delegates in Objective C

Tags:

objective-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?

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?

like image 989
Mr Snuffle Avatar asked Sep 05 '09 02:09

Mr Snuffle


2 Answers

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.

like image 97
Jeremy W. Sherman Avatar answered Oct 04 '22 04:10

Jeremy W. Sherman


An alternative to the notification center is to use an NSProxy subclass to forward messages, detailed in the posting at:

http://engineering.hoteltonight.com/handling-multiple-delegates-in-ios

The class HTDelegateProxy is available at:

https://github.com/hoteltonight/HTDelegateProxy

like image 31
Jacob Jennings Avatar answered Oct 04 '22 05:10

Jacob Jennings