Objective-C变量动态命名/引用在运行时

Tom*_*T39 2 variables objective-c

我有一个变量名引用了Objective C guru的问题.

假设我在表单上有6个UILabel,设置了属性,命名为myLabel1 - myLabel6

我想通过一个for循环并根据循环填充这些东西,但我不确定如何指定for循环变量并使其成为标签名称的一部分.

这是我想做的事情:

for (int LP = 0; i <5)
{

    labelLP.text = [NSString stringWithFormat:@"My label number:%d", LP};
}
Run Code Online (Sandbox Code Playgroud)

我不确定的是如何引用标签并附加LP int并在我的循环中使用它.我敢肯定有办法做到这一点,不知道怎么样......任何人?

jac*_*doe 5

你总是可以利用objective-c的动态运行时:

id var = object_getIvar(self,class_getInstanceVariable([self class], [[NSString stringWithFormat:@"label%d",LP] UTF8String]));
Run Code Online (Sandbox Code Playgroud)

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html