Set the enum or #define tag for an object in the interface builder

When you create a lot of the same object, a good design partner sets a tag for the identifier, therefore:

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

To be easier and more beautiful to understand the code, you can create an enumeration.

 typedef enum { MyTextField1 = 1, MyTextField2 } allTextField; 

So, you do not put only a number and you can set the tag in this way:

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

Than in any delegate function you can treat it more easily

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

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

enter image description here

But if I install it, I will get:

enter image description here

Is there no way in the Builder interface to set a tag other than a number?

+4
source share
3 answers

There is no way to use an enumeration in IB. Because IB files are serialized objects. And when they are loaded at runtime, they cannot reference the enumeration name.

+2
source

Define vs Enum

#define might be easier to type because you don't need = or need to worry about the end , between each element. But you have an additional #define that is less than DRY. I use enumerations, but in any case this is normal.

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

vs.

 #define SongNameLabelTag 1 #define PlayButtonTag 2 

Naming convention

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

eg. SongNameLabelTag , PlayButtonTag

Tag Values ​​in IB

The goal is to not give the element the same tag.

It’s unpleasant for me to recall the last tag that I used when working in IB.

Keeping your data up to date seems like a solution, but it is annoying, and there are several times when it will not work.

eg. Moving an item to another controller. (I usually extract element groups to separate view controllers. This will cause your numbering to start with strange numbers or collide with existing elements.)

When prototyping and working quickly slow me down, I need to add each element tag as #define . I like to work as fast as possible :)


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

Most importantly, they must be quickly available!

Just tag 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

+1
source

No, this is impossible, and it is a shame, because technically it can be implemented. Especially with all this parser.

So far this is the only reason I manually code some menus and avoid the interface designer.

0
source

Source: https://habr.com/ru/post/1385106/


All Articles