The BEST method is probably to simply use the notification center. Rather than a @protocol
, put a NSString * const
you're using for the notification:
NSString * const kMyNotificationString @"MyNotificationString"
Now, when the would-be delegates need to respond, it's as simple as:
[[NSNotificationCenter defaultCenter] postNotificationName:kMyNotificationString
object:nil];
Any object that needs to respond to this notification can #import
this header file (so it gets the NSString * const
-ed notification name and then and start listening to the notification as such:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMethod)
name:kMyNotificationString
object:nil];
Just don't forget to remove observers when (or before) you dealloc.
Alternatively, there's this approach... but it's not really that great, and I'd actually recommend against it.
Rather than a single delegate property, you could have a NSArray
property that handles the "delegates" (make sure you have an "addDelegate" method that only adds weak references to the array). Then, when you need all of these objects to respond to a change:
for (id<MyProtocol> object in self.arrayOfDelegates) {
if ([object respondsToSelector:@selector(myMethod)]) {
[object myMethod];
}
}