Rodrigo
Rodrigo

Reputation: 12683

Set enum or #define tag to an object in interface builder

When you build a lot of equal object is a good design partner set a tag to identifier, so:

UITextField *object1, *object2;
//Initialize it
[object1 setDelegate:self];
[object2 setDelegate:self];
[object1 setTag: 1]; 
[object2 setTag: 2];

To be more easy and "beautiful" understand the code, you can create a enum.

typedef enum {
      MyTextField1 = 1,
      MyTextField2
} allTextField;

So, you will not put just a number and can set tag in this way:

[object1 setTag: MyTextField1];
[object2 setTag: MyTextField2];

Than in any delegate function you can treat it more easy

- (BOOL)textFieldShouldClear:(UITextField *)textField {
   switch(textField.tag) {
       case MyTextField1: return YES;
       case MyTextField2: return NO;
   }
}

But, when you will build in Interface Builder in XCode, you can set the tag in this field:

enter image description here

But if I set it, I will receive:

enter image description here

There is no way to set the tag anything than a number in Interface Builder?

Upvotes: 4

Views: 1271

Answers (3)

vaughan
vaughan

Reputation: 7465

Define vs Enum

#define's can be easier to type because you don't need the = or have to worry about the trailing , between each element. But you do have the extra #define which is less DRY. I use enums, but either way is fine.

typedef enum {
      SongNameLabelTag = 1,
      PlayButtonTag = 2
} MyViewControllerTags;

vs.

#define SongNameLabelTag 1
#define PlayButtonTag 2

Naming Convention

I name tags in format: <name><short type description>Tag

e.g. SongNameLabelTag, PlayButtonTag

Tag values in IB

The goal here is to avoid giving an element the same tag.

I find it frustrating to remember the last tag I used when working in IB.

Keeping your defines up to date seems like a solution but this is annoying and there are some times when it won't work.

E.g. Moving an element to another view controller. (I usually extract groups of elements to separate view controllers. This will lead to your numbering starting at weird numbers or clashing with existing elements.)

When prototyping and working quickly it slows me down to have to add each element tag as a #define. I like to work as quickly as possible :)


My solution is to use random numbers. The max you can use is NSIntegerMax (32-bit) which is 2147483647.

Most importantly they need to be quickly available!

Just bookmark this link every time you need some random tags:

http://www.random.org/integers/?num=100&min=1&max=999999999&col=1&base=10&format=html&rnd=new

Upvotes: 1

SystematicFrank
SystematicFrank

Reputation: 17261

No, it is not possible, and it is a shame because technically it could be implemented. Specially with all that syntax analyzer.

So far that is the only reason for which I manually code some menus and avoid the interface builder.

Upvotes: 0

V1ru8
V1ru8

Reputation: 6147

There is no way to use an enum in IB. Because IB files are serialized objects. And when they get loaded at run time, they can't make the reference to the name of the enum.

Upvotes: 2

Related Questions