0

I have declared an enum datatype like:

typedef enum TagTypes
{
   BUTTON_TAG_1 = 1,
   BUTTON_TAG_2,
   BUTTON_TAG_3,
   NEW_TAG

}ButtonTag;

typedef enum TagType
{
   LABEL_TAG_1 = 1,
   LABEL_TAG_2,
   NEW_TAG
}LabelTag;

I wanted to find the corresponding tag of the button or label through this tag value as

(clickedbutton.tag == ButtonTag.BUTTON_TAG1) or (changingLabel.tag == LabelTag.LABEL_TAG_1)

but this syntax doesn't seem to be possible in Obj C, it throws me error saying Expected Identifier or ")"

Is there a way i can select tagNames by specifying tagDatatype like:

LabelTag.LABEL_TAG_2, ButtonTag.BUTTON_TAG2, ...

Thanks for any help

clickedbutton.tag == BUTTON_TAG1 will work, but I prefer to use it like tagName.tagValue , so that i can have same tagValues for different tag sets say tagValue "NEW_TAG" in both LabelTag and ButtonTag.

2
  • are you not getting error: redeclaration of enumerator? Commented Mar 17, 2012 at 10:25
  • @ParagBafna: Is there any way to get around that error? Commented May 10, 2013 at 14:25

4 Answers 4

5

I believe it follows the same convention as it does in C where you just write

if (clickedbutton.tag == BUTTON_TAG1)

without specifying the enum type. You only have to specify the type when its a variable.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, that will work, but I prefer to use it like tagName.tagValue , so that i can have same tagValues for different tag sets say tagValue "NEW_TAG" in both LabelTag and ButtonTag. Thanks for your help.
@Prabhakaran Paulthiravium since there is already a sufficient answer, I'd like to add that a tag is an integer typ. So all you've got to do is to initialize your buttons properly: button1.tag = BUTTON_TAG_1. And since you're using an enum you may like rename the tag's names to something more meaningful, else you're trading a number for a symbol named as a number.
0

Have a look at Apple's headers. They simply prefix all enum entries with the enum's name, e.g. UIViewAnimationCurveEaseInOut in the enum UIViewAnimationCurve. I suggest you do the same.

Comments

0

you may use switch-case

switch(LabelTag)
{
   case : LABEL_TAG_1

   break;

   case : LABEL_TAG_2

   break;
}

Comments

0

If you really want to use the form of LabelTag::LABEL_TAG_2 you can use objective-c++ mode (change your file extension to .mm) and do this:

class FirstEnum
{
    public: 
    enum { a, b, c } ;
} ;

class SecondEnum
{
    public:
    enum { a, b, c } ;
} ;

Then in your code can can refer to LabelTag::a for example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.