4

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?

Community
  • 1
  • 1
ohho
  • 50,879
  • 75
  • 256
  • 383
  • 1
    I've used the buttons `title` property a lot to encode grouping information. If you want the title to be displayed on the button, simply hook up the buttons to an `IBOutletCollection`, and loop through each of the titles to get the relevant display and grouping information, and parse the string accordingly. – msgambel Jan 21 '13 at 02:32

3 Answers3

6

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

Nate Chandler
  • 4,533
  • 1
  • 23
  • 32
  • It is indeed, if you want to have a third party object which manages the buttons. However it's not quite a good solution if you want the button itself to know about its type/state (which is an OO principle btw -> encapsulation). – Rad'Val Jan 21 '13 at 02:49
  • 1
    @ValentinRadu The button doesn't operate on that level of abstraction. It cares about images, titles, targets, and actions. Whether it's a numeric button, or a square root button, or what have you, is really a concern of the controller layer, and forcing the button to take responsibility for that is moving controller logic into the view. – Carl Veazey Jan 21 '13 at 04:53
  • 1
    @CarlVeazey the state or kind of a button is not "controller logic". But this question was never about good design, so let's agree to disagree. – Rad'Val Jan 21 '13 at 05:06
3

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.

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
0

Perhaps I misunderstand your question but the tag property is definitely settable for UIButtons through XCode/Interface Builder.

enter image description here

spring
  • 18,009
  • 15
  • 80
  • 160
  • It's my fault of not clarifying what _friendly_ means. `tag` is definitely settable in IB but it's only a number. I am looking for some intuitive alternatives. For example, what about `color`? – ohho Jan 21 '13 at 02:24