How to store supporting information inside UIButton via Interface Builder?

I am creating a custom keyboard that has about 50 UIButton keys. I would like to put some grouping information for each key, such as numbers, alphabets, cursors, etc.

The first thing that comes to mind is the tag field (100 for alphabets, 200 for numbers, ... etc.)

Another option is a category with associative links .

However, both methods are not very convenient for the Builder interface. Any other option?

+4
source share
3 answers

You wrote

I would like to put some grouping information for each key, such as numbers, alphabets, cursors, etc.

Why not use IBOutletCollection s?

You can declare arrays for each of the "numbers, alphabets, cursors":

 @property (nonatomic) IBOutletCollection(UIButton) NSArray *numberButtons; @property (nonatomic) IBOutletCollection(UIButton) NSArray *letterButtons; @property (nonatomic) IBOutletCollection(UIButton) NSArray *cursorButtons; 

and easily connect them in the interface constructor:

enter image description here

+6
source

Perhaps the cleanest way to do this and handle everything in the same subclass / category using User Defined Runtime Attributes

enter image description here

In the above example, you must have at least one method:

 -(void) setLocalizedKey:(NSString *) key; //(NSString *) localizedKey; this is optional 

or property:

 @property(nonatomic, strong) NSString * localizedKey; 

declared and implemented (or @synthesize ) in your UIButton subclass or category if you like associative links (or you don't need memory or you don't have access to the class itself).

In the above example, the key value will be HOW_IT_WORKS for this particular instance. This is how I usually handle localization, but should also serve your purpose.

+3
source

I may have misunderstood your question, but the tag property is definitely configurable for UIButtons via UIButtons / Interface Builder.

enter image description here

0
source

All Articles