How to know which UIButton was pressed?

How to know which UIButton is pressed when

there are more than one UIButton in a view?

 

Suppose there are two buttons in a view

Now in the identity inspector window for button0 set the tag value=0

Set the tag value for button1 as 1 in the same way as for button0

 

Now here is the coding part

Link the following method with both the buttons in the Interface Builder

-(IBAction)action:(id)sender
{
if ([sender tag]==0) {
NSLog(@"button0");
}
else
{
NSLog(@"button1");
}
}

Here sender gets the identity of the button object which is pressed.

By usingĀ  [sender tag] we get to know which button was pressed.

Same thing applies for more than two buttons provided different tag values should be used for each UIButton object.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>