How to detect button press between multiple buttons in lens c
I have 4 buttons (b1, b2, b3, b4) and a label (lab). Now I want to display the title of a button in a label when a specific button is clicked. I did this using four methods (IBAction), one for each button. But I want to do it with Method 1 (IBAction). The problem is how to determine which button is pressed ??? I knew a method similar to the getBytitle method. But I need a better solution. Can anyone help ??? I also need an answer on how to identify a button in the control.Advanced thanx segment for an answer.
source to share
Look in IB, the attibutes button tag field 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. You can now check which button was clicked by checking 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>
}
}
source to share
The button that triggers the action is passed as the sender. Your method probably looks something like this:
- (IBAction) buttonPressed: (id) sender;
A button sender
is a button, so if you want to display the name of the button in a label, all you have to do is:
- (IBAction) buttonPressed: (id) sender
{
label.text = [sender currentTitle];
}
It should be.
source to share