Graphics Programs Reference
In-Depth Information
For the More Curious: Pros and Cons of Callback Options
A callback, as you may remember from Chapter 4 , is a chunk of code that you supply in
advance of an event occurring. When that event goes down, the chunk of code gets ex-
ecuted. In this chapter, you have seen that blocks can be used as callbacks. Other ap-
proaches to callbacks you have seen in this topic are delegation, target-action pairs, and no-
tifications. Each one has benefits and drawbacks compared to the others. This section will
expand on these benefits and drawbacks so that you can pick the appropriate one for your
own implementations.
First, let's note that each of these approaches to callbacks are design patterns that transcend
their implementations in Cocoa Touch. For example, the target-action pair design pattern is
implemented by UIControl , but this does not mean you have to use UIControl to use
the design pattern. You could create your own class that kept a pointer to a target object and
a SEL for the message that object would be sent when some event occurred.
A callback has two major components: the process of registering it and the code for the
callback. When registering a callback using delegation, target-actions, or notifications, you
register a pointer to an object. This is the object that will receive messages when events oc-
cur. Additionally, both target-actions and notifications require a SEL that will be the mes-
sage that is sent to the object. Delegation, on the other hand, uses a pre-defined set of meth-
ods from a delegate protocol to regulate which messages get sent.
In these three callback design patterns, the code for the callback is in a distinct method im-
plementation ( Figure 27.6 ).
Figure 27.6 Callback design patterns
 
Search WWH ::




Custom Search