目标 C:什么是“(id)发件人”?
在一些IBAction
中我看到:
- (IBAction)pushButton:(id)sender;
This (id)sender
when do I use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在一些IBAction
中我看到:
- (IBAction)pushButton:(id)sender;
This (id)sender
when do I use it?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
Matt Galloway 描述了
(id) 的含义发送者
在 iPhone Dev SDK 论坛上的操作如下:请参阅
UIControl 类参考了解更多信息细节。
举个例子,UITextField 有一个委托,它在 UITextField 编辑结束时触发:
Matt Galloway described the meaning of
(id) sender
in actions on the iPhone Dev SDK forums thusly:See the
UIControl Class Reference for more details.
An example for that, UITextField has a delegate which triggers when the UITextField editing ends:
代码示例:
Code example:
“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.
它是 目标动作机制,这是对象相互通信的一种方式。为了响应事件(例如鼠标单击),一个对象(通常是某种控件)向另一个对象发送一条消息。发送者被称为“发送者”,接收者被称为“目标”,消息被称为“动作”。
您可以在目标的消息处理程序中使用它来从发送方获取有关操作的附加信息。
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.
我是从 Rabskatran 那里学到的。但我想纠正唯一说“发送者”的部分是变量的名称。它应该是(来自Apple文档 - https: //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html):
“操作消息调用的方法具有特定的签名:保存引用的单个参数到发起操作消息的对象;按照惯例,此参数的名称是 sender,例如,
所以它是一个参数!
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,
SO IT IS A PARAMETER!
下面是 (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