In some IBAction
I saw:
- (IBAction)pushButton:(id)sender;
This (id)sender
我什么时候使用它?
马特·加洛韦描述了(id) sender http://iphonedevsdk.com/forum/iphone-sdk-development/13295-about-id-sender.html在 iPhone Dev SDK 论坛上的行动如下:
(id)sender 是将消息发送到该选择器的对象。就像在委托函数中,您可以将控制传递给函数等。
如果您有 2 个对象正在调用该选择器并且您想要区分它们,则可以使用此选项。当然,您可以只使用两个不同的函数,但使用一个函数通常更干净且代码重复更少。
See the
UIControl 类参考 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html更多细节。
举个例子,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];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)