开发者

Objective c and events [duplicate]

This question already has answers here: Closed 11 years ago.
开发者_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
]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜