开发者_Python百科Possible Duplicate:
Multiple Delegates in Objective C
C# programmer here.
I have a subclass and I want it to fire an event that multiple classes can subscribe to.
I had been using delegates to do this type of thing, but the problem I have is that only one class can subscribe to the delegate.
What is the pattern is objective c to have multiple observers?
Use the NSNotificationCenter to register and listen to events:
NSNotificationCenter Class Reference:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
Notification Programming Topics:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Notifications/Introduction/introNotifications.html%23//apple_ref/doc/uid/10000043i
[ [ NSNotificationCenter defaultCenter ]
postNotificationName: @"notificationName"
object: someObject
]
And then, to listen:
[ [ NSNotificationCenter defaultCenter ]
addObserver: self
selector: @selector( someMethod: )
name: @"notificationName"
object: theObject
]
精彩评论