0

I have some code where there may or may not be objects in the Array... this is the code I am dealing with:

        oServices1.text = CustomServicesArray[0];
        oServices2.text = CustomServicesArray[1];
        oServices3.text = CustomServicesArray[2];
        oServices4.text = CustomServicesArray[3];
        oServices5.text = CustomServicesArray[4];
        oServices6.text = CustomServicesArray[5];
        oServices7.text = CustomServicesArray[6];
        oServices8.text = CustomServicesArray[7];
        oServices9.text = CustomServicesArray[8];
        oServices10.text = CustomServicesArray[9];
        oServices11.text = CustomServicesArray[10];
        oServices12.text = CustomServicesArray[11];
        oServices13.text = CustomServicesArray[12];
        oServices14.text = CustomServicesArray[13];
        oServices15.text = CustomServicesArray[14];
        oServices16.text = CustomServicesArray[15];
        oServices17.text = CustomServicesArray[16];
        oServices18.text = CustomServicesArray[17];
        oServices19.text = CustomServicesArray[18];
        oServices20.text = CustomServicesArray[19];
        oServices21.text = CustomServicesArray[20];
        oServices22.text = CustomServicesArray[21];
        oServices23.text = CustomServicesArray[22];

Rather than check each and every array object for nil, is there a way I can take the oServices*xx*.text UIFields and put them into some kind of array so I can just use a loop?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • possible duplicate of [Dynamic Variable Names iOS](http://stackoverflow.com/questions/9936183/dynamic-variable-names-ios) – jscs Sep 16 '13 at 21:18

3 Answers3

2

Are you aware of reflexivity? With KVC you could save up much code and time:

for(int i=1; i<=23; i++) {
    NSString* key= [NSString stringWithFormat: @"oServices%d"i];
    // Remember that variables should start with a lowercase letter
    [[self valueForKey: key] setText: customServicesArray[i-1] ]; 
}

But if you don't want to bind all these variables in your storyboard/xib file (even this may be too much), just set the tag of each text field in the order that you want (from 1), so that you can get them back using viewWithTag:

// From the UIViewController
for(int i=1; i<=23; i++) {  // Consider defining a constant instead of 23
    [[self.view viewWithTag: i] setText: customServicesArray[i-1] ];
}

I consider this last solution better because you avoid binding so many variables.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • This is exactly what I was looking for... I forgot about the string concatenation... works like a champ! Thank you all for your suggestions! SD – SpokaneDude Sep 16 '13 at 21:49
0

You can use an OutletCollection to hold oServices and loop on that. Note however that outlet collections are not sorted so you would need to sort them beforehand (on the tag criteria, or location for example).

For ordering see this question.

Community
  • 1
  • 1
David
  • 9,635
  • 5
  • 62
  • 68
  • Wouldn't they be in "sorted" order if I used an index to refer to the object in the OutletCollection and the same index to refer to the customServicesArray? – SpokaneDude Sep 16 '13 at 20:32
  • @spokane-dude last time I checked, there was a bug in ordering of outletCollections, so the index wouldn't necessarily reflect the data you want to present. See the linked question in my answer. – David Sep 16 '13 at 20:36
  • I just tried to create the OrderedCollection... there are 24 UITextFields, but when I do an NSLog to see what's inside the OrderedCollection, I get this: **servicesOUtletCollection: ( "; layer = >" )** So how do I verify that I have the correct fields in the OC? – SpokaneDude Sep 16 '13 at 20:47
  • @spokane-dude The collection is an array, but you only have one value. It looks as if you haven't linked all your oServices to the collection in IB. – David Sep 16 '13 at 20:58
  • Here's what I did: I captured each UITextField, holding the CMD key, then took the CTRL key and dragged it to the Connections Inspector, creating the OC. Was I supposed to do it another way? How about doing it programmatically? – SpokaneDude Sep 16 '13 at 21:09
0

Set the tag property of the UITextFields to their corresponding ordinal in the array. The default value of tag is 0, so you may need to set the tag property to ordinal + 1 if there are other views in the parent view of your UITextFields. On the parent view of your text fields, you can use the viewWithTag: method to retrieve the appropriate UITextField.

Fruity Geek
  • 7,351
  • 1
  • 32
  • 41