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:UIControlEventTouc开发者_开发百科hUpInside];
[addButtonObj setTitle:component.componentValue forState:UIControlStateNormal];
aMethod
is my method name and i want to send multiple parameter to this.
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
[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.
精彩评论