Iphone 如何向选择器发送多个参数?

Iphone 如何向选择器发送多个参数?,iphone,objective-c,selector,Iphone,Objective C,Selector,您好,有人能告诉我如何将多个参数发送到选择器吗。我以编程方式创建了一个按钮,我想发送该按钮选择器的三个参数。请帮帮我 下面是我写的代码: UIButton *addButtonObj = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [addButtonObj addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside]; [ad

您好,有人能告诉我如何将多个参数发送到选择器吗。我以编程方式创建了一个按钮,我想发送该按钮选择器的三个参数。请帮帮我

下面是我写的代码:

UIButton *addButtonObj = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[addButtonObj addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchUpInside];
[addButtonObj setTitle:component.componentValue forState:UIControlStateNormal];

aMethod
是我的方法名,我想向它发送多个参数。

将目标添加到
ui按钮时,有三种可能传递数据:

- (IBAction)aMethod;                                       // no data passed
- (IBAction)aMethod:(id)sender;                            // passed sender obj
- (IBAction)aMethod:(id)sender forEvent:(UIEvent *)event;  // passed sender obj + event
您可以给按钮一个标签,并在
aMethod:
方法中要求它:

- (IBAction)aMethod:(id)sender {

    UIButton *theButton = (UIButton*)sender;
    if(theButton.tag == 42) {

        // call my fancy method with 3 params!
    }
 }
也许你应该提供更多关于你最终想要达到的目标的细节:)

致以最良好的祝愿,
Christian

将目标添加到
ui按钮时,有三种可能传递数据:

- (IBAction)aMethod;                                       // no data passed
- (IBAction)aMethod:(id)sender;                            // passed sender obj
- (IBAction)aMethod:(id)sender forEvent:(UIEvent *)event;  // passed sender obj + event
您可以给按钮一个标签,并在
aMethod:
方法中要求它:

- (IBAction)aMethod:(id)sender {

    UIButton *theButton = (UIButton*)sender;
    if(theButton.tag == 42) {

        // call my fancy method with 3 params!
    }
 }
也许你应该提供更多关于你最终想要达到的目标的细节:)

致以最良好的祝愿, 基督教徒

但是按钮操作的目标可能会收到一个id(通常命名为sender)

在方法调用中,sender应该是您案例中的按钮,允许您读取属性,例如它的名称、标记等

但是按钮操作的目标可能会收到一个id(通常命名为sender)


在方法调用中,发送方应该是您案例中的按钮,允许您读取属性,如名称、标记等。

在iOS上,操作方法可以有三个可能的签名:无参数、类型为
id
的单个参数或类型为
id
UIEvent*
的两个参数;所有三个签名的返回类型均为
void
。这一点很好。不知道最后一个(使用
id
UIEvent
)。将编辑我的答案。Cheerron iOS,一个操作方法可以有三个可能的签名:无参数、类型为
id
的单个参数,或类型为
id
UIEvent*
的两个参数;所有三个签名的返回类型均为
void
。这一点很好。不知道最后一个(使用
id
UIEvent
)。将编辑我的答案。干杯