views:

20

answers:

2

I can't understand the logic og button tags. Can someone tell me how to use button tags?

For eg. There are two buttons on my view and I want to print something depending on their tags like:

if(button.tag==???)x{

}etc.
A: 

Yeah you can use the tag to retrieve the UIButtons, and apply the same logic with UIVIews (have a look at this method remembering that UIButton inherits from UIView). Specifically where do you have problems? Can you post some code/pseudo-code of yours?

rano
i found the solution thanks
pankaj
+1  A: 

When you create the button, you can set it's tag.

myButton1.tag = 0;
myButton2.tag = 1;

Or if you're using interface builder, there's a field in the inspector to set the tag.

I assume you've linked the buttons to call the same action when they're pressed, or else you wouldn't be needing to distinguish by tag, so your method should look like:

- (IBAction)buttonPressed:(id)sender
{
    UIButton *aButton = (UIButton *)sender; // we know the sender is a UIButton object, so cast it

    if (aButton.tag == 0)
    {
        // button 1 pressed
    }
    else if (aButton.tag = 1)
    {
        // button 2 pressed
    }
}
Jasarien
thanks its working ,thank
pankaj
Good to hear. If this answer was helpful, consider voting it up using the arrows next to the answer. If it solved your problem, consider marking it as accepted, so that other will know this answer works.
Jasarien