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?
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
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With