1
  1. 据我所知,[self setDate:...]均值self是接收者,setDate:是消息。那么发件人是谁?

  2. 我设置了一个带有 action 的按钮(IBAction)buttonPressed:(id)sender。我知道这个按钮是目标,buttonPressed是动作。那么哪个是可以的sender

4

4 回答 4

4

1) As I known, [self setDate:...] mean self is receiver and setDate: is message. So which is the sender ?

The sender is the object—if any—that sent the message.

So which one is that?

  1. You wrote the message in a statement.
  2. You wrote that statement within a function, class method, or instance method.

    • If it was a function, then there is no object that was the sender.
    • If it was a class method, arguably the class is the sender (depending on how much you consider classes to be objects).
    • 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.

于 2013-05-17T05:22:35.423 回答
0

1)是的,self是消息的接收者,setDate:是消息(方法调用)。没有“发件人”。无论如何,您所说的“发件人”是什么意思?

2)在代码中,设置按钮调用buttonPressed:方法是这样完成的:

[someButton addTarget:someTarget action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];

someTarget通常是self. “目标”是具有“动作”方法的对象。该方法如下所示:

- (void)buttonPressed:(UIButton *)button {
}

“someButton”是“发送者”,因为它是具有事件的按钮。

于 2013-05-16T03:27:41.657 回答
0

发件人是执行任务的对象....假设您的示例(IBAction)buttonPressed:

如果您按下按钮,则该按钮正在发送随后的任何代码。看我的例子

- (IBAction)pushButton:(id)sender {
UIButton *pushedButton = (UIButton *)sender;
[button setTitle:@"Press me to Send" forState:UIControlStateNormal];
}
于 2013-05-16T01:55:07.257 回答
0

实际上,在objective-c 消息系统中没有任何正式的“发送者”概念。

  • (IBAction)buttonPressed:(id)sender。

    这里的 sender 只是普通的方法参数,不是目标 c 消息系统的一部分。只是在 Appkit 或 UIKIt 中命名的方法参数。

  • [self setDate:...] 这里的“self”是接收者,setDate: 是消息。

请注意:在普通话中“发送消息”意味着三件事:发送者、接收者和消息。其中发送者是包含消息的函数或代码块。但该类比不适用于目标 c。

于 2014-05-06T11:27:24.520 回答