-4

I have some variables like vh1 vh2 vh3 etc. Is it possible in a for-loop to count with the i variable?

I mean something like for(int i = 1; blablabla) { [[vh + i] setBackGroundColor blablabla];}

Regards

Edit: vh1 etc. are UILabels!!!

Sebastian
  • 7,670
  • 5
  • 38
  • 50
Phil
  • 943
  • 2
  • 6
  • 18

4 Answers4

3

While this is possible through introspection, if you have such variables you better put them in an NSArray, and access them with an index.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • But the variables are for UILabels, so is it possible to put them in an array? – Phil Feb 24 '13 at 07:58
  • Do u have an example for me? Until now i do `IBOutlet UILabel *vh1;` (same with the others) How can i put this into an array? – Phil Feb 24 '13 at 08:10
  • 2
    You can create an IBOutletCollection in interface builder, or just add them to an array in your code (using `@[vh1,vh2,vh3]` is simplest). – jrturton Feb 24 '13 at 08:17
1

As other answerers have noted, with the new array syntax you can quite easily construct an array with all your objects in it, but it will keep the old values even if you subsequently change the values of the original ivars. That may or may not be what you are after.

If you are hell-bent on keeping your variables as single objects (as opposed to arrays,) then you can use key-value coding to access them programmatically. Key-value coding is also known as KVC.

The method that does it is valueForKey: and can be used both on self and other objects.

MyClass *obj = ... // A reference to the object whose variables you want to access

for (int i = 1; i <= 3; i++) {
    NSString *varName = [NSString stringWithFormat: @"var%d", i];

    // Instead of id, use the real type of your variables
    id value = [obj valueForKey: varName];

    // Do what you need with your value
}

There is more about KVC in the docs.

In the interest of completeness, the reason this direct access works, is because a standard KVC compliant object inherits a class method called accessInstanceVariablesDirectly. If you don't want to support this direct access, then you should override accessInstanceVariablesDirectly so it returns NO.

Monolo
  • 18,205
  • 17
  • 69
  • 103
0

You can access each values the following code.

UILabel *label1;
UILabel *label2;
UILabel *label3;
NSArray *array = @[label1, label2, label3];
for (int i = 0; i<3; i++) {
    [array objectAtIndex:i];
}

Adding values to NSArray is available for initializing it. If you want to add values later, you can use NSMutableArray.


I modified my code.

UILabel *label1 = [[UILabel alloc] init];
UILabel *label2 = [[UILabel alloc] init];
UILabel *label3 = [[UILabel alloc] init];
NSArray *array = @[label1, label2, label3];
for (int i = 0; i<3; i++) {
    UILabel *label = [array objectAtIndex:i];
    label.frame = CGRectMake(0, i*100, 150, 80);
    label.text = [NSString stringWithFormat:@"label%d", i];
    [self.view addSubview:label];
}
akiniwa
  • 617
  • 8
  • 18
  • Thanks for all the answers. Iam creating them with `IBOutlet UILabel *vh1;` etc. and then trying your way to at them in an array but it doesn't work. `Expected blablabla before '=' token` :/ – Phil Feb 24 '13 at 08:27
  • Do you write this code on viewController? You want to show UILabels on display? – akiniwa Feb 24 '13 at 08:35
  • Yes, it will be an iPhone app and i write this code on viewController – Phil Feb 24 '13 at 08:41
  • but i declare them in the .h file with IBOutlet and wanna use them in the .m file with the for loop and this doesnt really works :( – Phil Feb 24 '13 at 09:32
  • So, you should initialize this way, in the .h file :UILabel *label1;, in the .m file:label1 = [[UILabel alloc] init]; – akiniwa Feb 24 '13 at 11:41
0

If you are loading the UILabels from XIB, you can use IBOutletCollection.

Declare property:

@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;

Now you can link multiple labels in XIB to this property. Then in -viewDidLoad (after loading the XIB), your array is populated and you just use simple for-in:

for (UILabel *label in self.labels) {
    label.backgroundColor = ...
}
Tricertops
  • 8,492
  • 1
  • 39
  • 41