我的iOS应用程序不会遍历我的字符串

时间:2013-03-27 17:11:44

标签: ios objective-c nsstring

我意识到这是一个非常基本的问题,其他帖子似乎没有帮助,因为我对此很陌生。我很好奇为什么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?";

    }
}

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分: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);

 }
}