【发布时间】: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 来完成吗?
【问题讨论】:
-
labelArray 有一个动态大小,对吧?您想将该数组中的每个元素映射到 UILabel 吗?但是您没有动态数量的标签?如果你真的想要这个,你最终可以使用 - (id)valueForKey:(NSString *)key (developer.apple.com/mac/library/documentation/Cocoa/Reference/…)
标签: iphone objective-c nsarray objective-c-blocks iboutlet