简体   繁体   中英

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

I have a UIViewController , and in that I have an array of questions that I pull from a sqlite3 query. I am then using a for loop to iterate through each question in the array to change the UILabel.text to display the question on the screen. This is actually working for the first question in the array!

I then have four buttons for answers. I want to make it so if one of the buttons is pressed, the answer is saved and the next question in the loop updates the UILabel.text .

The four answers never change as it is more of a survey than answers, so one of the answers is "I agree" or "disagree", so the button text never changes. Is this possible?

I have been on here and Google to find a way to link the button pressed with completing each iteration of the loop without any luck.

Why are you iterating through questions and changing UILabel's text? Shouldn't be it changed only on tapping one of the survey buttons?

If I got you correctly, you should do following:

1) Declare three properties in your controller: NSArray *questions, NSMutabelArray *answers, NSInteger currentIndex;

2) Init/alloc them in viewDidLoad (except currentIndex, of course, set it to 0).

3) Fill up questions array with your question strings.

4) Set text to UILabel, label.text = questions[currentIndex];

5) create IBAction method and link it to all survey buttons.

6) in IBAction method, insert button's title to answers array and show next question.

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

I hope you will understand the code.

In short, yes this is possible.

You'll first want to keep track of the question that your user is currently on. You can do this by storing an index in an instance variable or, if you plan on allowing the user to open the app and start from where they left off, you can use NSUserDefaults, which writes to disk and will persist.

// 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

By storing the index in NSUserDefaults, you can grab it in ViewDidLoad, and start from where the user last left off:

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

To store your answer, you could add a method for your buttons called 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...

}

You can add this method to your buttons like so

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

You could then write a method to increment questionIndex now that an answer button has been pressed.

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

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

}

You could then call a separate, final method to update the question label.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM