gpt4 book ai didi

objective-c - objective-c : what is a "(id) sender"?

转载 作者:IT老高 更新时间:2023-10-28 11:48:51 24 4
gpt4 key购买 nike

在一些IBAction中我看到了:

- (IBAction)pushButton:(id)sender;

这个(id)sender什么时候用?

最佳答案

Matt Galloway 描述了 (id) sender 的含义因此,在 iPhone Dev SDK 论坛上采取行动:

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

查看 UIControl Class Reference了解更多详情。


一个例子,UITextField 有一个委托(delegate),当 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];
}
}

关于objective-c - objective-c : what is a "(id) sender"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5578139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com