views:

51

answers:

1

Hi,

I started learning Iphone development. As i'm reading about the cocoa development framework, few principal notions came around and with it a little bit of confusion. I want to make sure I understand things right and to get a little clarification :

Delegate - basically the strategy design pattern. I set my class delegate and data source (which is also a delegate-like object) , and later on call methods on those delegate objects.

Notification - basically the observer design pattern, where the delegate is automatically registed to all the delegating object notifications.

Target/action - This it where i'm mostly confused. There are the "SetAction"" and "SetTarget" methods for UI objects. As I understand, if I connect a method using IB (by streching a line betweeen the UI object to the file owner), the method is the action and the file owner is the target. Does it mean that all the UI methods' (events) have to be directed to the same targer ? (As I only have SetTarget method which doesn't accept a Selector or anything similar as another argument). The same question remains regarding actions. How can I stretch lines between many methods to one file owner if there is only one action (As the function SetAction imply) at a time. I'm looking to understand how this mechanism works.

Thanks

+1  A: 

Delegate - basically the strategy design pattern.

I don't agree. The Strategy pattern uses delegation but involves more than what Cocoa understands as delegation. If you have the Gang of Four book: it discusses delegation in the introduction (pp. 20-21). But I think you got it basically right: the delegating object calls methods on its delegate, either to inform it about something (a state change, an event) or to ask the delegate for data or if/how to proceed. The important thing is that delegate and delegating object are only loosely coupled: when implementing the delegating object, you don't have to care about the delegate's class.

Target/action - This it where i'm mostly confused.

Objects that have a setTarget: and setAction: method usually only have a single action/event (such as UIBarButtonItem, which only responds to one event: a single tap). Otherwise, the class should support setting different targets/actions for different events. See UIControl and its subclasses as an example. There, you assign target/action pairs with -addTarget:action:forControlEvents: and you are free to assign multiple targets for different events and also assign multiple targets for the same event.

Ole Begemann
Great , make sense.However, it that's the case how target/action is different then the observer pattern (or notification in case of cocoa), besides the fact that notification go through the notification center , and here every control class can act as observed class ?
Idan