Reputation: 229
As I known, [self setDate:...]
mean self
is receiver and setDate:
is message. So which is the sender ?
I set a button with action (IBAction)buttonPressed:(id)sender
. I understood that this button is the targer and buttonPressed
is the action. So which is can be sender
?
Upvotes: 1
Views: 348
Reputation: 454
Actually, In objective- c messaging system don't have any formal notion of "sender".
(IBAction)buttonPressed:(id)sender.
Here sender is just ordinary method argument ,not a part of objective c messaging system.Just a way argument is named in Appkit or UIKIt.
[self setDate:...] here "self" is receiver and setDate: is message.
Please note : in ordinary speech "sending a message" implies three thing: sender,receiver and message.Where sender is function or block of code that contain message. But that analogy is not applicable for objective c.
Upvotes: 0
Reputation: 96333
1) As I known,
[self setDate:...]
meanself
is receiver andsetDate:
is message. So which is the sender ?
The sender is the object—if any—that sent the message.
So which one is that?
You wrote that statement within a function, class method, or instance method.
If it was an instance method, whichever object had that method called on it—i.e., received that message—is the sender of the setDate:
message.
For example, if you sent your setDate:
message from an instance method named buttonPressed:
in an NSWindowController subclass, then your window controller—the instance (object) that received the buttonPressed:
message—is the object that sent the setDate:
message.
Of course, if the receiver of the message is self
, then you are sending the message to yourself, so you are both the receiver and the sender.
2) I set a button with action (IBAction)buttonPressed:(id)sender. I understood that this button is the targer and buttonPressed is the action.
Nope! The button is not the target; the button has a target, which is another object. The target is the object that receives the action message.
Think of an archery target. The target doesn't send the arrow! It receives the arrow.
So which is can be sender ?
The object that is referred to by the sender
variable generally is the object that sent the message.
Usually, it is a control (such as a button) or menu item: when pressed, the control or menu item sends its action message to its target, passing itself (the control or menu item) as the “sender”.
So your button would send a message to its target, the message being whichever one is set as the button's action, and because it's sending the message, it would pass itself as the sender
.
The only reason to look at the sender
at all is to examine the sender's tag
or representedObject
. An action method is about the only kind of method that may need to care at all about the sender (which is why it has this argument), and even then, usually not.
Upvotes: 4
Reputation: 318824
1) Yes, self
is the receiver of the message and setDate:
is the message (the method call). There is no "sender". What do you mean by "sender" anyway?
2) In code, setting up the button to call the buttonPressed:
method is done like this:
[someButton addTarget:someTarget action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
someTarget
is typically self
. The "target" is the object that has the "action" method. The method looks like:
- (void)buttonPressed:(UIButton *)button {
}
The "someButton" is the "sender" since it is the button that had the event.
Upvotes: 0
Reputation: 10602
The sender is the object preforming the task....assuming your example (IBAction)buttonPressed:
If you are pressing a button, that button is doing the sending of whatever code follows. See my example
- (IBAction)pushButton:(id)sender {
UIButton *pushedButton = (UIButton *)sender;
[button setTitle:@"Press me to Send" forState:UIControlStateNormal];
}
Upvotes: 0