Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store auxiliary information inside a UIButton, via Interface Builder?

I am building a custom keyboard, in which there are about 50 UIButton keys. I'd like to put some grouping information for each key, e.g., numbers, alphabets, cursors, etc.

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

Another option is by means of a category with Associative References.

However, both methods are not very Interface Builder-friendly. Any other option?

like image 850
ohho Avatar asked Jan 15 '23 11:01

ohho


2 Answers

You wrote

I'd like to put some grouping information for each key, e.g., numbers, alphabets, cursors, etc.

Why not use IBOutletCollections?

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

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

and easily hook them up in interface builder:

enter image description here

like image 188
Nate Chandler Avatar answered Jan 21 '23 19:01

Nate Chandler


Probably the cleanest way to do it and handle everything in the same subclass/category is by using User Defined Runtime Attributes

enter image description here

For the example above, you should 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 fancy associative references (or you don't need storage or don't have access to the class itself).

In the example above, the value of key will be HOW_IT_WORKS for that specific instance. This is the way I usually handle localisation, but should serve your purpose too.

like image 22
Rad'Val Avatar answered Jan 21 '23 19:01

Rad'Val