按下按钮后更新 for 循环内的 UILabel.Text

Updating UILabel.Text inside of a for loop after a button is pressed

我有一个 UIViewController,其中我有一系列从 sqlite3 查询中提取的问题。然后我使用 for 循环遍历数组中的每个问题以更改 UILabel.text 以在屏幕上显示问题。这实际上适用于数组中的第一个问题!

然后我有四个答案按钮。我想这样做,如果按下其中一个按钮,答案将被保存,循环中的下一个问题将更新 UILabel.text.

四个答案永远不会改变,因为它更像是一项调查而不是答案,所以其中一个答案是 "I agree" 或 "disagree",因此按钮文本永远不会改变。 这可能吗?

我一直在这里 Google 找到一种方法来 link 按下按钮并完成循环的每次迭代,但没有任何运气。

为什么要遍历问题并更改 UILabel 的文本?不应该只在点击调查按钮之一时更改吗?

如果我没听错,你应该执行以下操作:

1) 在控制器中声明三个属性:NSArray *questions、NSMutabelArray *answers、NSInteger currentIndex;

2) Init/alloc 它们在viewDidLoad中(currentIndex除外,当然要设置为0)。

3) 用您的问题字符串填写问题数组。

4) 将文本设置为 UILabel,label.text = questions[currentIndex];

5) 创建 IBAction 方法并将其link用于所有调查按钮。

6) 在 IBAction 方法中,将按钮的标题插入答案数组并显示下一个问题。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.questions = {your questions array};
    self.answers = [[NSMutableArray alloc] init];
    self.currentIndex = 0;

}

- (IBAction)btnClicked:(id)sender {

    UIButton *btn = (UIButton *)sender;
    NSString *title = btn.titleLabel.text;
    [self.answers addObject:title];

    currentIndex++;
    label.text = questions[currentIndex];
}

我希望你能理解代码。

简而言之,是的,这是可能的。

您首先要跟踪您的用户当前提出的问题。您可以通过在实例变量中存储索引来做到这一点,或者,如果您计划允许用户打开应用程序并从他们离开的地方开始,您可以使用 NSUserDefaults,它写入磁盘并将持续存在。

// In the interface of your .m file
int questionIndex;

// In viewDidLoad of your controller, however this will start for index 0, the beginning of your questions array
questionIndex = 0

通过将索引存储在 NSUserDefaults 中,您可以在 ViewDidLoad 中抓取它,并从用户上次离开的地方开始:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber   numberWithInt:questionIndex] forKey:@"questionIndex"];

要存储您的答案,您可以为您的按钮添加一个名为 answerTapped: 的方法:,

- (void)answerTapped:(UIButton *)answerButton
{
   //  Grab the answer from the text within the label of the button
   //  NOTE: This assume your button text is the answer that you want saved
   NSString *answer = answerButton.titleLabel.text;

   // You can use your questionIndex, to store which question this answer was for and you can then take the answer and store it in sqlite or where you prefer...

}

您可以像这样将此方法添加到您的按钮

[answerButton addTarget:self action:@selector(answerTapped:) forControlEvents:UIControlEventTouchUpInside];

现在您可以编写一个方法来在按下回答按钮后递增 questionIndex。

- (void)incrementQuestionIndex
{
  // Increment index
  questionIndex += 1;

  // Update and save value in UserDefaults
  [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:questionIndex] forKey:@"questionIndex"];

}

然后您可以调用一个单独的最终方法来更新问题标签。

- (void)updateQuestionLabel
{

  // Grab the question using the index (omit this line and go straight to the next if storing the index in an iVar)
  questionIndex = [[[NSUserDefaults standardUserDefaults] objectForKey:@"questionIndex"] integerValue];

  // Grab the question using the index.  Assumes you have a questions array storing your questions from sqlite.
  NSString *question = [questions objectAtIndex:questionIndex]; 

  // Update the question UILabel 
  [questionLabel setText:question];
}