Graphics Programs Reference
In-Depth Information
There are certain situations where one of these design patterns works better than the oth-
ers.
Target-action is used when you have a close relationship between the two objects (like a
view controller and one of its views) and when there are many instances that call back.
For example, a single interface controlled by one controller may have many buttons. If
those buttons only knew how to send one message (e.g., buttonTapped: ), there would
be mass confusion in the implementation of that method (“Uhhh... which button are you
again?”). Each button having its own action message (e.g., redButtonTapped: )
makes life easier.
Delegation is used when an object receives many events and it wants the same object to
handle each of those events. You've seen many examples of delegation in this topic be-
cause delegation is very common in Cocoa Touch. Delegation uses a protocol that defines
all of the messages that will be sent, so you do not have control over the names of these
methods, but you do not have to register them individually like with target-action pairs,
either.
Notifications are used when you want multiple objects to invoke their callback for the
same event and/or when two objects are not related. Consider an application that has two
view controllers in a tab bar controller. They don't have pointers to each other, unlike two
Search WWH ::




Custom Search