0

I have twelve text fields as you can see below:

IBOutlet UITextField *ce_1;
IBOutlet UITextField *ce_2;
IBOutlet UITextField *ce_3;
....
IBOutlet UITextField *ce_12;

All I have to do is to set an existing object in an array in each of the variables that are responsible for the text fields, I'm currently doing as follows:

ce_1.text = myArray[1];
ce_2.text = myArray[2];
ce_3.text = myArray[3];
....
ce_12.text = myArray[12];

Not to be writing a lot, I thought I'd put this in an automated way within a loop as follows:

for(i=1;i<13;i++){
ce_[i].text = myArray[i];
}

But this command does not work the way I expected, so I would like your help to try to solve my idea and put it into practice, is there any way of doing this?

Wain
  • 118,658
  • 15
  • 128
  • 151
  • possible duplicate of [ObjC equivalent of PHP's "Variable Variables"](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables), [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), [Syntax help: variable as object name](http://stackoverflow.com/q/7940809) [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs Apr 16 '14 at 19:27

4 Answers4

1

Research and start using IBOutletCollection. It will give you an array of text fields that you can build in your storyboard XIB.

Note that you may need to consider the order of the array, and that you might want to sort it (possibly based on the tag of each view).

Technically, you could use string formats and KVC to do what you're currently trying to but it is far from ideal.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

You can't just replace ce_1 ce_2 ce_3 with ce_[i] it doesn't work that way. You can only use [number] with an nsarray variable (or decendents).

for example:

NSArray* myArray = @[@1];
NSLog(@"%@", myArray[0]);

You might want to look into IBOutletCollection in order to achieve something similar to what you're looking for.

However, contrary to other answers here IBOutletCollection are ordered by how you link them in the interface builder.

Refer to this for IBOutletCollections: How can I use IBOutletCollection to connect multiple UIImageViews to the same outlet?

Community
  • 1
  • 1
Isaac Paul
  • 1,959
  • 2
  • 15
  • 20
  • it works exactly 'that way' if you do it exactly like in your answer instead of: for(i=1;i<13;i++){ ce_[i].text = myArray[i]; } Nevertheless, KVC is too 'magical' to be used in clean code. It breaks encapsulation too. – Isaac Paul Apr 16 '14 at 19:52
  • well it won't work that way if you use an IBOutletCollection, then there would be no point of making 13 properties … – A'sa Dickens Apr 16 '14 at 19:53
  • Yes, IBOutletCollection > (KVC + 13 properties) – A'sa Dickens Apr 16 '14 at 19:54
  • 2
    KVC is too magical? Then I guess entire portions of the framework work on magic then. >_ – Léo Natan Apr 16 '14 at 19:57
0

You can use IBOutletCollection. You can also use key-value coding:

for(NSUInteger i = 0; i < 13; i++)
{
    [[self valueForKey:[NSString stringWithFormat:@"ce_%u", i]] setText: myArray[i]];
}

This will give you what you want.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
0

The way I like to handle these situations is creating a temporary array containing all the text fields:

NSArray *allFields = @[_ce_1, _ce_2, _ce_3, ...];
NSInteger i = 0;
for (UITextField *tf in allFields)
{
    tf.text = myArray[i];
    i++
}

IBOutletCollection also work but sometimes it gets hard to figure out when you come back to your project which label is #3 or #5 and such... I find this works better for me usually :)

Taum
  • 2,511
  • 18
  • 18