0

I have Objective C class with properties:

@property (weak, nonatomic) IBOutlet UILabel *labelText1;
@property (weak, nonatomic) IBOutlet UILabel *labelText2;
@property (weak, nonatomic) IBOutlet UILabel *labelText3;
@property (weak, nonatomic) IBOutlet UILabel *labelText4;
@property (weak, nonatomic) IBOutlet UILabel *labelText5;
@property (weak, nonatomic) IBOutlet UILabel *labelText6;

Is there the way to enumerate it in code? Something like:

for (int i = 1; i <= 6; i++)
{
    theCell.labelText i.text = @"some value";
}

Thanks!

user3742622
  • 1,037
  • 3
  • 22
  • 40
  • 2
    For the specific example above, I would just use a outlet collection instead of outlets for individual UILabels. Then you could iterate over all the labels through the outlet collection which would be an array of UILabels – user3435374 Jan 26 '15 at 18:22

4 Answers4

1

You can use KVC to access those properties.

  NSArray *allProperties = @[@"labelText1", @"labelText2", @"labelText3", @"labelText4", @"labelText5", @"labelText6"];

for (NSString *aProperty in allProperties) {
   UILabel *label = [theCell valueForKey:aProperty];
   label.text = @"some value";
}

This would yield you the result you want. Anyway, I would suggest you to create IBOutletCollection for such UI element or an array of only those that you want to iterate through.

Sandeep
  • 20,908
  • 7
  • 66
  • 106
1

You can create an IBOutletCollection of UILabel properties:

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

In your storyboard or .xib file, control-drag from the UILabel elements to this property.

Then in your code you can perform the following:

    [self.myControllerLabels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
    label.text = @"some value";
}];
Steve
  • 1,840
  • 17
  • 20
0

Yes, but I wouldn't recommend using it in a real application. The objective-C runtime is pretty open: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html

There are a number of methods that will give you the properties.

ahwulf
  • 2,584
  • 15
  • 29
  • Thanks! I am quite newbie in objective-C. Could you explain why it's a bad idea? I am curious about it, cause I had written the code but I came up with idea that it will look better with automatically enumeration of different situations... – user3742622 Jan 26 '15 at 18:30
  • The above suggestions are much better in real applications. Using the runtime might leave you open to a future version (unlikely now since Swift is the future) where they are no longer available. – ahwulf Jan 26 '15 at 21:33
0

You could use @selector() by calling performSelector:withObject:afterDelay: on theCell and passing in the NSString as the object.

NSString *selectorName;
for (int i = 1; i <= 6; i++)
{
    selectorName = [NSString stringWithFormat:"setLabelText%i", i];
    [theCell performSelector:@selector(selectorName) withObject:@"some value"];
}
Armin
  • 1,150
  • 8
  • 24