Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is sender?

I can't find this answer anywhere. What does it mean when there's a sender parameter in a method header? Does it represent the instance that called it, or the method that called it?

like image 889
marty Avatar asked May 16 '10 04:05

marty


1 Answers

What does it mean when there's a sender parameter in a method header? Does it represent the instance that called it, or the method that called it?

Look at the type of the argument. Chances are, it's id. That's the type of an object pointer. You're correct that it is the instance that sent the message.

You can pass a message selector to a message, but the type for that is SEL, not id. Likewise, you can pass a method implementation to a message, but the type for that is IMP, not id.

Methods that take a single sender argument are typically action methods, usually identified by the IBAction return type. As zoul said, IBAction expands to void for the compiler, which tells it that the method does not return a value. The reason to have IBAction is that Interface Builder looks for methods with IBAction as the return type and detects them as actions you can wire a control up to.

For more information for Cocoa (Mac OS X), see “The Target-Action Mechanism” in the Cocoa Fundamentals Guide and the Control and Cell Programming Topics for Cocoa.
For more information for Cocoa Touch (iPhone/iPod touch/iPad), see “The Target-Action Mechanism” in the UIControl class reference.

like image 119
Peter Hosey Avatar answered Oct 06 '22 10:10

Peter Hosey