目标 C:什么是“(id)发件人”?

发布于 2024-10-30 13:55:34 字数 153 浏览 6 评论 0 原文

在一些IBAction中我看到:

- (IBAction)pushButton:(id)sender;

This (id)sender when do I use it?

In some IBAction I saw:

- (IBAction)pushButton:(id)sender;

This (id)sender when do I use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帝王念 2024-11-06 13:55:34

Matt Galloway 描述了 (id) 的含义发送者在 iPhone Dev SDK 论坛上的操作如下:

(id)sender 是将消息发送到该选择器的对象。就像在委托函数中,您可以将控制传递给函数等。

如果您有 2 个对象正在调用该选择器并且您想要区分它们,则可以使用此选项。当然,您可以只使用两个不同的函数,但使用一个函数通常更干净且代码重复更少。

请参阅
UIControl 类参考了解更多信息细节。


举个例子,UITextField 有一个委托,它在 UITextField 编辑结束时触发:

-(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];
   }
}

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];
   }
}
美胚控场 2024-11-06 13:55:34
(id)sender is the object which sent the message to that selector.

代码示例:

- (IBAction)submitButton:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setEnabled:NO];
    [button setTitle:@"foo" forState:UIControlStateDisabled];
}
(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];
}
2024-11-06 13:55:34

“sender”是变量的名称。

“(id)”表示变量的类型是“id”,代表“任何对象”(如果你愿意,你可以将其视为对象层次结构的顶部。

方法的名称是pushButton: 并需要 1 个任何类型的参数。

此方法将链接到 UI 上的按钮。不需要它,有时您需要访问该 UIButton 来更改他的属性。

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

御守 2024-11-06 13:55:34

它是 目标动作机制,这是对象相互通信的一种方式。为了响应事件(例如鼠标单击),一个对象(通常是某种控件)向另一个对象发送一条消息。发送者被称为“发送者”,接收者被称为“目标”,消息被称为“动作”。

您可以在目标的消息处理程序中使用它来从发送方获取有关操作的附加信息。

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.

千寻… 2024-11-06 13:55:34

我是从 Rabskatran 那里学到的。但我想纠正唯一说“发送者”的部分是变量的名称。它应该是(来自Apple文档 - https: //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html):

“操作消息调用的方法具有特定的签名:保存引用的单个参数到发起操作消息的对象;按照惯例,此参数的名称是 sender,例如,

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

所以它是一个参数!

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!

执手闯天涯 2024-11-06 13:55:34

下面是 (id)sender 将标签信息从多个按钮传递到一个 IBAction 的示例。该视频演示了 (id) 发送者的实际概念,我发现这非常有用。

iPhone 编程 - (id)发件人说明

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文