Objective C: what is a "(id) sender"?
Asked Answered
E

6

39

In some IBAction I saw:

- (IBAction)pushButton:(id)sender;

This (id)sender when do I use it?

Eastwood answered 7/4, 2011 at 8:30 Comment(1)
Note you can format lines as code by indenting them four spaces. A leading -, by contrast, gives you a list element, as you can see in your question. The "{}" button in the editor toolbar does this for you. Edit your question and try it out. Click the orange question mark in the editor toolbar for more information and tips on formatting.Boxberry
E
40
(id)sender is the object which sent the message to that selector.

Code example:

- (IBAction)submitButton:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setEnabled:NO];
    [button setTitle:@"foo" forState:UIControlStateDisabled];
}
Enteric answered 7/4, 2011 at 8:44 Comment(6)
ok, but you can use this id sender only for the "button"; if I should to modify a textfield, what is the way?Eastwood
@blackguardian: you mean you want to cast the id to a UITextField and then change it's text?Lefler
ok it's very clear, the function that receive an id how it's getting called? from delegate? or you call it explicitly? show me the function header please.Lefler
Just a quick question. Why do you have to set the sender as another UIButton? Can't you just do something like [sender setEnabled:NO];Carce
@theAmateurProgrammer: maybe he want to know which control exactly sent that IBAction, like if this is the last UITextField do Login function now.Lefler
@theAmateurProgrammer You can, but casting to UIButton * gives more information to the compiler (it can check if the selector corresponds to a method declared in UIButton/superclasses of UIButton) and to the IDE/Xcode (it can narrow down the list of suggested method completions).Kilogram
L
64

Matt Galloway described the meaning of (id) sender in actions on the iPhone Dev SDK forums thusly:

(id)sender is the object which sent the message to that selector. It's like in the delegate functions where you have the control passed in to the function, etc.

You'd use this if you had 2 objects which were calling that selector and you wanted to distinguish between them. Of course, you could just use two different functions, but it's often cleaner and less duplication of code to use one function.

See the UIControl Class Reference for more details.


An example for that, UITextField has a delegate which triggers when the UITextField editing ends:

-(IBAction) editingEnded:(id) sender {
   // the cast goes here, lets assume there's more than one UITextfield 
   // in this Owner and you want to know which one of them has triggered
   // the "editingEnded" delegate
   UITextField *textField= (UITextField*)sender;
   if(textField == iAmTheLastTextField)
   {
     // for example login now.
     [self login];
   }
}
Lefler answered 7/4, 2011 at 8:30 Comment(1)
This answer is actually better than the answer that's been accepted…Tomboy
E
40
(id)sender is the object which sent the message to that selector.

Code example:

- (IBAction)submitButton:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setEnabled:NO];
    [button setTitle:@"foo" forState:UIControlStateDisabled];
}
Enteric answered 7/4, 2011 at 8:44 Comment(6)
ok, but you can use this id sender only for the "button"; if I should to modify a textfield, what is the way?Eastwood
@blackguardian: you mean you want to cast the id to a UITextField and then change it's text?Lefler
ok it's very clear, the function that receive an id how it's getting called? from delegate? or you call it explicitly? show me the function header please.Lefler
Just a quick question. Why do you have to set the sender as another UIButton? Can't you just do something like [sender setEnabled:NO];Carce
@theAmateurProgrammer: maybe he want to know which control exactly sent that IBAction, like if this is the last UITextField do Login function now.Lefler
@theAmateurProgrammer You can, but casting to UIButton * gives more information to the compiler (it can check if the selector corresponds to a method declared in UIButton/superclasses of UIButton) and to the IDE/Xcode (it can narrow down the list of suggested method completions).Kilogram
I
5

"sender" is the name of the variable.

"(id)" means that the type of the variable is "id", that stands of "any object" (You can see it as the top of the object hierarchy if you want

The name of the method is pushButton: and require 1 parameter of any kind.

This method will be linked to a button on the UI. The delegate of this UI will receive this call and will have a reference to the UIButton that has made the call. Sometimes you don't need it, sometimes you need to have access to that UIButton to change his properties for instance.

Ingurgitate answered 7/4, 2011 at 8:35 Comment(0)
B
1

It's part of the target-action mechanism of Cocoa, which is one way objects can communicate with each other. In response to an event (such as a mouse click), one object (usually a control of some kind) sends a message to another object. The sender is called, well, "sender", the receiver is the "target" and the message is the "action".

You can use it in the target's message handler to get additional information about the action from the sender.

Boxberry answered 7/4, 2011 at 8:38 Comment(2)
This would be more useful if there was an example of precisely how to get the additional information :)Lozenge
@GForty: not possible, since id is a generic object. In general, it's an NSObject, but the type is no more certain than that. If you want additional information, you must know what interfaces the object implements and send the appropriate message(s) to it (as in gontnull's sample, where the sender is assumed to be a button).Boxberry
R
0

I learnt from Rabskatran. But i wouldlike to correct the only part that said "sender" is the name of the variable. It should be (from Apple documentation - https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html):

"The methods invoked by action messages have a specific signature: a single parameter holding a reference to the object initiating the action message; by convention, the name of this parameter is sender. For example,

  • (void)moveToEndOfLine:(id)sender; // from NSResponder.h"

SO IT IS A PARAMETER!

Riotous answered 15/4, 2011 at 18:6 Comment(0)
S
0

Here's an example of (id)sender passing tag information from several buttons to one IBAction. This video demonstrates the concept of (id) sender in action, which I found to be very useful.

iPhone Programming - (id)sender explained

Swanhilda answered 3/3, 2012 at 1:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.