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?
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.
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