views:

1496

answers:

6

I am developing for iPhone the app I am developing has many buttons and I want all buttons to call the same method but with different parameter

for example I want button1 to call the method mymethod with the parameter "foo"

and button2 should call the same method but with parameter "bar"

Thanks for help

+1  A: 

Give your various UIButton instances different tag property values.

In your IBAction method -myMethod:, you might then do something like:

- (void) myMethod:(id)sender {
    switch (sender.tag) {
        case (firstButtonTag):
           doFooStuff;
           break;
        case (secondButtonTag):
           doBarStuff;
           break;
        // etc.
    }
}

The values firstButtonTag and secondButtonTag can be stored in an enum if you want to make this easy to maintain.

Alex Reynolds
+8  A: 

The so called "IBActions" must have one of these signatures:

-(void)action;
-(void)actionWithSender:(id)sender;
-(void)actionWithSender:(id)sender event:(UIEvent*)event;

You cannot add any other parameters. Nevertheless you can use sender (which is button1 or button2 in your case) to get the parameter:

-(void)actionWithSender:(UIButton*)sender {
   NSString* parameter;
   if (sender.tag == 1)   // button1
     parameter = @"foo";
   else                   // button2
     parameter = @"bar";
   ...
}
KennyTM
+1  A: 

You can't pass parameters through an IBAction. What I usually do is give the buttons the unique tag in IB. THe tag is an integer value so I u then use a simple lookup table to convert the tag to some value.

In this case, three buttons but tags 1 to 3:

- (IBAction) buttonPressed: (UIButton*) sender
{
    static const NSString* names = { @"Foo", @"Bar", @"Baz" };
    id tag = [sender tag];
    if (tag >= 1 && tag <= 3) {
        NSLog(@"Button pressed is %@", names[tag]);
    }
}
St3fan
A: 

You can't.

Create the actions for all your buttons, and from those call the method you want with the correct parameter.

Actions methods usually only receive one parameter, like so:

- (IBAction)someAction:(id)sender;

Since it's your control calling this method, you cannot change what is passed as the sender argument (which is usually the control itself).

pfandrade
A: 

You don't. The only parameter is the sender object, which you may use to have a different behavior, but what I'd do is define 2 action methods, which simply in turn call the same method with a different parameter, i.e. you'd have:

- (IBAction)button1:(id)sender
{
    [self doStuff:kButton1];
}

- (IBAction)button2:(id)sender
{
    [self doStuff:kButton2];
}

- (void)doStuff:(ParamType)param;
{
    ...
}

In defense of that method (no pun intended), I'd add that it makes clearer when you review your UI with Interface Builder to see that different buttons actually have different effects, which is harder to tell if they all call whateverAction:

Pierre Lebeaupin
A: 

As others have mentioned you cannot pass your custom parameter into action method. If you do not like the solution using tags you may also subclass UIButton with your custom class and add your parameter there. (By I wouldn't bother and just use tags)

Vladimir