tags:

views:

1117

answers:

2

I have 4 button (b1,b2,b3,b4) and a label (lab).Now I want to display button title in label when a particular button is pressed.i did it with four (IBAction) method one for every button.But i want to do it with 1(IBAction) method.so the problem is to to how to identify which button is pressed??? i knew a method something like "getBytitle" method.But i need better solution.Can anyone help??? I also need answer about how to identify button in segment control.Advanced thanx for reply.

+2  A: 

The button that triggers the action gets passed as the sender. Your method probably looks a bit like this:

- (IBAction) buttonPressed: (id) sender;

The sender is the button, so that if you want to display the button title in a label, all you have to do is this:

- (IBAction) buttonPressed: (id) sender
{
    label.text = [sender currentTitle];
}

That should be it.

zoul
+2  A: 

Have a look in IB, the tag field of the button attibutes might be what you are looking for. Set each of the buttons you want to detect with a different integer tag value, and then set their IBActions to the same method. Now you can check which button was pressed by checking for the tag field in the sender

- (IBAction) buttonPressed: (id) sender
{
    switch ( ((UIButton*)sender).tag ){

       case 1:
               <something>
               break;
       case 2:
               <something else>
               break;

       default:
               <default something>
    }
}
Kevin