1

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?

Community
  • 1
  • 1
Mel
  • 2,055
  • 2
  • 26
  • 45
  • See this previous question: http://stackoverflow.com/questions/1382241/multiple-delegates-in-objective-c - answers are in much more depth than what you've got so far – lxt Apr 02 '11 at 16:17

1 Answers1

6

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
]
Macmade
  • 52,708
  • 13
  • 106
  • 123