在UIButton中的动作选择内传递参数 [英] Passing argument within action selection in UIButton
问题描述
鉴于您有方法
-(void)aSelector:(id)anyArgument;
并以编程方式设置了UIButton
And have set up a UIButton programmatically
UIButton *myButton = [[UIButton alloc] init]
好的,我的想法是,当您创建UIButton时,必须使用方法
Alright, I get the idea that when you create a UIButton, you have to use the method
[myButton addTarget:nil action@selector(aSelector:) for ControlEvents:UIControlEventTouchUpInside];
但是我该在哪里传递论点呢?我知道通常情况下,您会使用
But where do I pass the argument? I know that normally, you would use
[myButton withObject:anyArgument];
但是NSControl不允许这样做,但是可能因为选择器名称后面的冒号表明了这一点.
But NSControl doesn't allow for that but it is possible as the colon after the selector name indicates so.
推荐答案
通常,-aSelector:的定义如下
Typically, -aSelector: will be defined like this
- (void) aSelector:(id)sender
{
NSLog(@" sender = %@", sender);
}
,参数为myButton.请记住,您可能有多个按钮都在调用-aSelector方法:然后,您需要在运行时区分哪个是调用者(发送者).
and the argument will be myButton. Bear in mind that you might have several buttons all calling the method -aSelector: and you would, then, need to distinguish which was the caller (sender) at runtime.
这篇关于在UIButton中的动作选择内传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!