【问题标题】:How to construct IBOutlet names in code in order to change them?如何在代码中构造 IBOutlet 名称以更改它们?
【发布时间】:2010-08-03 12:40:52
【问题描述】:

假设我已经在 IB 中设置了几个 UILabel,并在代码中连接到 IBOulets(label1、label2、label3、label4)

我将如何在代码中创建这些变量名称,以便我可以在循环中更改每个变量的文本,其中标签取自 NSArray。

这是伪代码:

labelArray = [NSArray arrayWithObjects:@"this", @"array", @"has", @"a", @"random", @"amount", @"of", @"items", nil];
for (int i = 0; i < [labelArray count]; i++) 
{
    // labelx is the constructed name of the IBOutlet
    lablex.text = [labelArray objectAtIndex:i];

}

如何构造上面的“labelx”?这可以使用 Blocks 来完成吗?

【问题讨论】:

标签: iphone objective-c nsarray objective-c-blocks iboutlet


【解决方案1】:

你必须在某个地方初始化数组,使用

labelArray = [NSArray arrayWithObjects:@"this", @"array", @"has", nil];
uiLabelArray = [NSArray arrayWithObjects:label1,label2,label3,nil];

然后

for (int i = 0; i < [uiLabelArray count]; i++) 
{
    [uiLabelArray objectAtIndex:i].text = [labelArray objectAtIndex:i];
}

【讨论】:

  • 我已经有一个 labelArray。问题是在循环中将该数组转换为 UILabels 中的文本。
【解决方案2】:

您可以使用键值编码 (KVC)。它看起来像:

[[self valueForKey:[NSString stringWithFormat:@"label%d", i]] setText:[labelArray objectAtIndex:i]];

更多信息可以找到here

【讨论】:

  • Ewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww.
猜你喜欢
  • 2021-08-05
  • 2011-01-27
  • 2021-06-01
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2022-11-20
相关资源
最近更新 更多