-1

我意识到这是一个非常基本的问题,其他帖子似乎没有帮助,因为我对此很陌生。我很好奇为什么 A.)我的 for 循环不会遍历我的字符串(问题),为什么 B.)它只打印最后一个问题“你的姓氏是什么?” 当我单击目标操作按钮时,在我的 iOS 模拟器上。我所有的连接都是正确的,显然,如果它的构建,我的 .h 中的一切都很好。

感谢您的帮助!

- (IBAction) question;
{
    for (int i = 0; i < 5; i++)
    {
        questionLabel.text = @"Whats your name?";
        questionLabel.text = @"Whats your age?";
        questionLabel.text = @"Whats your height?";
        questionLabel.text = @"Whats your weight?";
        questionLabel.text = @"Whats your last name?";

    }
}
4

2 回答 2

7

它正在遍历您的字符串!它的速度如此之快,以至于你所看到的只是最后一个。

于 2013-03-27T17:12:37.740 回答
1

这些日志会给你一个确切的想法

- (IBAction) question;
 {
  for (int i = 0; i < 5; i++)
  {

    NSLog(@"Iteration: %d",i);

    questionLabel.text = @"Whats your name?";
    NSLog(@"%@",questionLabel.text);

    questionLabel.text = @"Whats your age?";
    NSLog(@"%@",questionLabel.text);

    questionLabel.text = @"Whats your height?";
    NSLog(@"%@",questionLabel.text);

    questionLabel.text = @"Whats your weight?";
    NSLog(@"%@",questionLabel.text);

    questionLabel.text = @"Whats your last name?";
    NSLog(@"%@",questionLabel.text);

 }
}

如果您想在同一个标​​签(questionLabel)上显示所有问题,请编写以下代码

 - (IBAction) question;
 {
   for (int i = 0; i < 5; i++)
   {

    NSLog(@"Iteration: %d",i);

    questionLabel.text = @"Whats your name?";
    NSLog(@"%@",questionLabel.text);

    questionLabel.text = [questionLabel.text stringByAppendingFormat:@"Whats your age?"] ;
    NSLog(@"%@",questionLabel.text);

    questionLabel.text = [questionLabel.text stringByAppendingFormat:@"Whats your height?"] ;
    NSLog(@"%@",questionLabel.text);

    questionLabel.text  = [questionLabel.text stringByAppendingFormat:@"Whats your weight?"] ;
    NSLog(@"%@",questionLabel.text);

    questionLabel.text  = [questionLabel.text stringByAppendingFormat:@"Whats your last name?"] ;
    NSLog(@"%@",questionLabel.text);

 }
}

如果你想在不同的标签上显示所有的问题,创建不同UILabel的,请写下面的代码

- (IBAction) question;
 {
  for (int i = 0; i < 5; i++)
  {

    NSLog(@"Iteration: %d",i);

    questionLabel.text = @"Whats your name?";
    NSLog(@"%@",questionLabel.text);

    questionLabel1.text = @"Whats your age?";
    NSLog(@"%@",questionLabel1.text);

    questionLabel2.text = @"Whats your height?";
    NSLog(@"%@",questionLabel2.text);

    questionLabel3.text = @"Whats your weight?";
    NSLog(@"%@",questionLabel3.text);

    questionLabel4.text = @"Whats your last name?";
    NSLog(@"%@",questionLabel4.text);

 }
}
于 2013-03-27T17:16:27.980 回答