打印一系列答案

时间:2012-03-15 06:12:51

标签: objective-c

如果我有这个:

int array[3] = {1,2,3};

for (int i=0; i<3; i++) {
    int answers = (array[i] + 1);

    NSLog(@"%d");

    NSString *text = [NSString stringWithFormat:@"%d ",answers];

    self.label.text = text;
 }

Nslog语句打印出所有答案。但标签只显示最后一个值。 :( 如何让标签显示所有三个答案?

谢谢。

3 个答案:

答案 0 :(得分:1)

int array[3] = {1,2,3};

// before you can set the label to something
self.label.text = @"answer:";

for (int i=0; i<3; i++) {
    int answers = (array[i] + 1);

    NSLog(@"%d", answer);

    NSString *text = [NSString stringWithFormat:@"%d ",answers];

    self.label.text = [self.label.text stringByAppendingString:text];
}

答案 1 :(得分:0)

它仅打印最后一个值的原因是它会覆盖前一个值。延迟看其他值

答案 2 :(得分:0)

int array[3] = {1,2,3};
NSMutableString *mutableStr = [[NSMutableString alloc]init];
for (int i=0; i<3; i++) {
   int answers = (array[i] + 1);
    NSLog(@"%d");

    NSString *text = [NSString stringWithFormat:@"%d ",answers];
    [mutableStr appendFormat:@"%@ ",text];
}

self.label.text = text;
[mutableStr release];