everywhere they say what IBAction is but do not explain what the whole 开发者_运维百科declaration means.
What is sender and id?
Regards, Namratha
According to my knowledge
IBAction
just impliesvoid
id
is a generic C type, which accepts objects of any kind/class. It is some what similar to void pointer inC
. We can useid
as a parameter of a method, in case, if objects of difference kind of classes will access that method.sender
, here,- is a human-named parameter name (you can name it anything)
- it refers the object which calls the method
changeGreeting:
An Example:
Lets assume the method changeGreeting:
is defined in ClassA
, and it reads like the following.
- (IBAction)changeGreeting:(id)sender {
[sender setText:@"Hello %@", [sender class]];
// Nothing can be returned from this method
}
And consider we have the following lines in Class B
.
// LINE 1
[aLabel addTarget:objOfClassA action:@selector(changeGreeting:) forControlEvents:UIControlEventTouchUpInside];
// LINE 2
[aTextField addTarget:objOfClassA action:@selector(changeGreeting:) forControlEvents:UIControlEventTouchUpInside];
In LINE 1, aLabel
is the sender and objOfClassA
is the receiver. And in LINE 2: aTextField
is the sender and objOfClassA
is the receiver. HereaLabel
& aTextField
are called senders because they are calling the method changeGreeting:
. And objOfClassA
is called receiver because objOfClassA's
changeGreeting:
method is called here.
When the user touches inside the aLabel
or aTextField
, their text
will be changed to Hello UILabel
or Hello UITextField
respectively.
The other Way:
We can also call changeGreeting:
method of objOfClassA
from Class B
like the following.
[objOfClassA changeGreeting:aLabel];
[objOfClassA changeGreeting:aTextField];
As the above code is self explanatory, objOfClassA
is the receiver. But aLabel
& aTextField
are not the senders. Because they are not invoking the method. Here they are just the arguments. Here the actual sender is Class B
, but aLabel
and aTextField
are passed in the argument (id)sender
. Either way the result of the method execution is same.
Maybe you should check out a Objective-C tutorial (such as this), as this is pretty basic.
The IBAction is defined as "void", so it means that the method does not have a return value. It is useful only for Interface Builder, so it knows which of your methods are actually action you can link.
The (id)sender part is a (the only) parameter to your method, which is named sender. (id) says that the type of the parameter can be any object:
From Objective-C FAQ:
What is id?
It's a generic C type that Objective-C uses for an arbitrary object. [...]
IBAction
is a keyword (#define
'd to void
) that Interface Builder looks for in your source code, and if it finds it as a return type, it will add it to the list of possibile actions you can wire up your IB elements to.
id
is a generic object type in Objective-C that denotes any Objective-C object.
and sender
is just the name of the id
-typed parameter.
Functionally saying: IBAction is a keyword to identify methods you could link to through Interface Builder. (id)Sender is necessary to identify the object is triggering the method in case you use the same method for several controls.
精彩评论