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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With