0

好的,所以我尝试使用数组和 for 循环给出 7 个不同的标签名称。

代码:

id huller[] = {hul18.text, hul17.text, hul16.text, hul15.text, hul14.text, hul13.text, hul12.text, hul11.text, hul10.text, hul9.text, hul8.text, hul7.text, hul6.text, hul5.text, hul4.text, hul3.text, hul2.text, hul1.text};

for (int i = 0;  7 > i; i++) {
    huller[i] = [NSString stringWithFormat:@"%i", x + 1];
    NSLog(@"%@", huller[i]);
}

NSLog 中的名称发生了变化,但在模拟器中没有变化。怎么了?

4

2 回答 2

0

如果您希望文本也更改,则必须手动设置文本。

NSArray *labels = //Array of labels;
for (int i = 0;  7 > i; i++) {
    huller[i] = [NSString stringWithFormat:@"%i", x + 1];
    labels[i].text = huller[i];
    NSLog(@"%@", huller[i]);
}
于 2012-10-28T13:27:09.010 回答
0

假设 hul18、hul17 等都是 UILabel 对象,那么这样做:

NSArray *labels = [ hul18, hul17, hul16, hul15, hul14, hul13, hul12, hul11, hul10, hul9, hul8, hul7, hul6, hul5, hul4, hul3, hul2, hul1 ];

// Change the text of every label in the array
for (int i = 0;  i < labels.count; i++) {
    UILabel *label = labels[i];
    label.text = [NSString stringWithFormat:@"%i", x + 1]; // Do you really want 'x' here or 'i'?
    NSLog(@"%@", label.text);
}
于 2012-10-28T15:54:09.827 回答