neel
neel

Reputation: 209

how to send multiple parameter to selector?

hello can anyone tell me how to send multiple parameter to selector. I have created one button programmatically and i want to send three parameters of that button selector. please help me.

below is the code which i wrote:

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

aMethod is my method name and i want to send multiple parameter to this.

Upvotes: 1

Views: 1646

Answers (2)

cweinberger
cweinberger

Reputation: 3588

When adding a target to your UIButton, there are three possibilities for passing data:

- (IBAction)aMethod;                                       // no data passed
- (IBAction)aMethod:(id)sender;                            // passed sender obj
- (IBAction)aMethod:(id)sender forEvent:(UIEvent *)event;  // passed sender obj + event

You could give your button a tag and ask for it in your aMethod: method:

- (IBAction)aMethod:(id)sender {

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

        // call my fancy method with 3 params!
    }
 }

Maybe you should provide more details on what you're finally want to achieve :).

Best Regards, Christian

Upvotes: 1

visakh7
visakh7

Reputation: 26390

[yourButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];

but the target of a button action probably receives an id (usually named sender).

- (void) buttonPress:(id)sender;

Within the method call, sender should be the button in your case, allowing you to read properties such as it's name, tag, etc.

Upvotes: 0

Related Questions