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

时间:2016-02-02 17:15:37

标签: ios objective-c uilabel

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

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

这四个答案永远不会改变,因为它更像是一个调查而不是答案,因此其中一个答案是“我同意”或“不同意”,因此按钮文本永远不会改变。 这可能吗?

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

2 个答案:

答案 0 :(得分:0)

为什么要迭代问题并更改UILabel的文字?不应仅在点击其中一个调查按钮时更改?

如果我找对你,你应该这样做:

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

2)在viewDidLoad中初始化/分配它们(currentIndex除外,当然,将其设置为0)。

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

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

5)创建IBAction方法并将其链接到所有调查按钮。

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];
}

我希望你能理解这些代码。

答案 1 :(得分:0)

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

您首先要跟踪用户当前所处的问题。您可以通过在实例变量中存储索引来实现此目的,或者,如果您计划允许用户打开应用程序并从停止的位置开始,则可以使用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];
}
相关问题