When I began using Objective C, coming from ActionScript, I was a little overwhelmed at the myriad ways that a child component can send messages to its parent. ActionScript seems sort of easy now, since generally (at least since AS3) most of this communication is done with events! The following is a comparison of the various types of child->parent communication that are employed by AS3 and Objective C/Swift.
Callbacks
A callback is basically when a parent tells a child which of its methods it wants the child to call when a certain event occurs.
ActionScript
Callbacks were more common in AS2 than AS3, but some still lingered around where the adobe team never got around to updating the API, for example in NetStream:
var ns:NetStream = new NetStream(nc); ns.client = {}; ns.client.onCuePoint = ns_onCuePoint; function ns_onCuePoint(item:Object):void { trace(item.name + "\t" + item.time); }
In this case, the child ns notified its parent of a cuepoint by calling the callback method defined in its ‘onCuePoint’ variable.
Objective C/Swift
Objective C and Swift perform callbacks in several ways, I’m going to look at these in the following posts:
Part 2: Actions
Part 3: Delegates
Part 4: Blocks/Closures
And then in Part 5 I’ll come back to talk about our familiar friend Event!
[…] Part 1: Callbacks […]