如何使用点表示法中的字符串访问属性?

iOS*_*Guy 0 syntax properties objective-c ios

查看以下代码:

@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"
Run Code Online (Sandbox Code Playgroud)

请注意我如何将标签的所有文本设置为"Hello".必须有一种更有效的方法来做到这一点.我希望我能以某种方式访问​​UIlabels(l112,1212等)的名称来这样做.这有意义吗?在我的实际应用程序中,我并没有真正将所有内容设置为"Hello",而是文本是计算的结果.此外,在我的实际应用程序中,我有超过3个标签要设置(有50+)这是我的实际代码使用重复if语句来访问每个UILabel:

-(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
}
Run Code Online (Sandbox Code Playgroud)

我希望能够像这样更有效地做到这一点:

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

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

请注意我的[UILabel withTitle:@"%@",stringID]部分用于点表示法.我知道这不起作用,但我想知道我怎么能正确写这个,所以stringID可以用来访问我需要的UILabel的名字?

nne*_*neo 5

尝试使用valueForKey:你的UILabel.

但是,更简洁的解决方案可能是将这些属性简单地存储在NSDictionary中.