iOSAppGuy iOSAppGuy -3 years ago 81
iOS Question

How can I access a property with a string inside of dot notation?

Check out the following code:

@property (weak, nonatomic) UILabel *l112;
@property (weak, nonatomic) UILabel *l212:
@property (weak, nonatomic) UILabel *l312:

((RBScorecardVC *)self.presentingViewController).l112.text = @"Hello"
((RBScorecardVC *)self.presentingViewController).l212.text = @"Hello"
((RBScorecardVC *)self.presentingViewController).l312.text = @"Hello"

Notice how I'm setting all the text of my labels to "Hello". There must be a more efficient way of doing this. I am hoping I can somehow access the names of the UIlabels (l112, 1212, etc) to do so. Does this make sense? In my actual app, I'm not really setting everything to "Hello" but instead the the text is the result of a calculation. Also, in my actual app I have more than 3 labels to set (there are 50+) Here is my actual code using a repetitive if statement to access each UILabel sparately:

-(IBAction) submitScore: (id)sender
self.stringID = ((RBScorecardVC *)self.presentingViewController).self.giveString;

if (self.stringID isEqualToString:@"l112")
{ ((RBScorecardVC *)self.presentingViewController).l112.text = [[NSString alloc]initWithFormat:@"%d", self.accumulator];} //l112 is the name of my UILabel
else if (self.stringID is EqualToString:@"l212")
{ ((RBScorecardVC *)self.presentingViewController).l212.text = [[NSString alloc]initWithFormat:@"%d", self.accumulator];} //l212 is the name of my UILabel

I am wanting to be able to do it more efficiently like this:

-(IBAction) submitScore: (id)sender
self.stringID = ((RBScorecardVC *)self.presentingViewController).self.giveString;

((RBScorecardVC *)self.presentingViewController).[UILabel withTitle:@"%@",stringID].text = [[NSString alloc]initWithFormat:@"%d", self.accumulator];}

Notice my [UILabel withTitle: @"%@", stringID] part used within dot notation. I know this doesn't work but I am wondering how I can write this correctly so stringID can be used to access the name of UILabel I need?

Answer Source

The other answers here deal with properties on objects. In this case you're trying to access the objects themselves. When you start asking yourself a question like this, you should just store all of the objects (in this case, labels), in an array property instead of individual properties for each label. Instead of thinking about/referencing labels as named variables like l112, think of them as indexes in the array:

UILabel *theLabelINeed = [self.myArray objectAtIndex:2]; [theLabelINeed callWhateverMethodINeedTo];

Or shorter, if you're coming from a python/ruby background and this is more readable for you:

[self.myArray[2] callWhateverMethodINeedTo];

And if you need to quickly access every label in the array, you can use fast enumeration in a for-loop:

for (UILabel *currLabel in self.myArray) {
    [currLabel callWhateverMethodINeedTo];

You could also use an NSDictionary instead, which lets you assign "keys" or names to objects.

NSDictionary *dict {"l112": label1, "l113": label2}; // or some other dictionary creation method, maybe a mutable dictionary or whatever.
UILabel *myLabel = [dict objectForKey:@"l112"];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download