Δευτέρα, 26 Μαρτίου 2012

Callbacks - short notes


For Objective-C programmers, there are three-forms that a callback can take.
  • -       TARGET-ACTION: before the wait begins you say “When x happens, send this particular message to that particular object,” The object receiving the message is the target. The selector for the message is the action.
  • -       Helper objects: Before the wait begins, you say “Here is a helper object that conforms to your protocol. Send it messages when things happen”. Helper objects are often known as delegates or data sources.
  • -       Notifications: There is an object called the notification center. Before the wait begins, you say to the notification center “This object is waiting for these sorts of notifications. When one of those notifications arrives, send the object this message”. When x happens, an object posts a notification to the notification center, and the center forwards it on to your object.

Types of Callbacks
Target-Action
sending ONE    callback   to ONE object
timers,simple user interface controls(buttons, sliders etc)
Helper objects
(delegate or data source)
sending MANY callbacks to ONE object
NSURLConnection.
The most common type of helper object is the delegate
Notifications
sending MANY callbacks to Multiple object
Notification center


Notes:
-       Notification centers do not own their observers (they typically remove itself from notification in its dealloc method)
-       Objects do not own their delegates or data sources (if you create an object that is delegate or data source, your object need to “clear” himself in its dealloc method). Exception: NSURLConnection owns its delegate
-       Objects do not own their targets. (if you create an object that is a target, your object should zero the target pointer in its dealloc method).Exception: NSTimer owns it’s target.



#:From The Big Nerd Ranch Guide Objective C programming

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου