Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between delegate/selector and target/action?

When writing Cocoa and implementing a class method that accepts a delegate and one or more selectors to use for its callbacks, does the terminology change from "delegate" and "selectors" to "target" and "action"? More to the point, is "delegate" limited only to when my class is the one getting called, but not when my class is doing the calling?

like image 639
Collin Allen Avatar asked Jun 14 '09 04:06

Collin Allen


2 Answers

Delegates are usually implemented using Protocols instead of selectors. This is a more formal way of communicating across classes, and is mostly useful when there is more than one method that may be needed.

Target/Action are generally used to correspond to an "event-like" situation, such as a click, a timer firing, etc.

like image 173
Ecton Avatar answered Sep 23 '22 12:09

Ecton


To elaborate on NilObject's answer, delegates are implemented via protocols with a series of 'standardized' methods. If you have multiple objects each of the same class sharing the same delegate you will be unable to distinguish between the sender. Hence you see -(void)delegateCallback:(id)sender the pointer of sender allows you to make this association.
In order to make this comparison a reference to sender will be required as a property or global variable, this also makes it easier if you are manually releasing your object as you can set the objects properties to nil before releasing (to prevent garbage pointers).

Alternatively you can use the selector method to pass in your own method for callback, this technique is good as you don't require the reference for a comparison with the :(id)sender and you can have a method callback for each object you initialize. On the flip side the lack of reference means that if you release this object you will be unable to set the properties to nil.

like image 29
Ospho Avatar answered Sep 21 '22 12:09

Ospho