views:

101

answers:

3

Hello,

I have multiple UIButtons in my app. I also use interfacebuilder. In my .h i have something like this

  IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;
    IBOutlet UIButton *button3;
    - (IBAction)buttonPressed;

Then In my m i want to do something like this

- (IBAction)buttonPressed {


if (theButtonIpressed == button1) 

{

// do something if 

}

}

The problem is I don't have something called "theButtonIpressed" so I cant do this. What should my if statement look like? I don't want to make a -(IBAction) for each button. Is there something that I can determine which button was pressed? Thanks!

Thanks,

-David

+2  A: 

Define your - (IBAction)buttonPressed to:

- (IBAction)buttonPressed: (UIButton *) buttonIpressed

Then it will work.

Alfons
Thanks also for your help.
bobbypage
+1  A: 

- (IBAction)buttonPressed:(UIButton*)button

But if you're doing something different for each button then the proper way to do it is to create separate IBActions.

macatomy
Thanks for your help.
bobbypage
A: 

You can also set the tag property for each button in the interface builder and then use it to find out which button was pressed.... This also means that you don't need to define all your button references (UIButton) and keep track of them in code....

- (void) doSomething:(id)sender {

    int buttonPressed = [sender tag];

    switch (buttonPressed) {
       case 0:....
       // etc
     }
}
Eric Schweichler