views:

27

answers:

3

I have an application with the following code listed below that displays random questions for the end user. I am trying to figure out how to have a user be able to go navigate backwards in an array that is displayed randomly. For instance, a user is going through the questions and accidentally advances past one and wants to go back, I want them to be able to just simply hit a back button. Could you provide any guidance on how to do this? Thank you very much!!!

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

  myArray5= [NSArray arrayWithObjects:           
      @"Question 1",
      @"Question 2",
      @"Question 3",
      @"Question 4",
      nil];

  int chosen = arc4random() % [myArray5 count]; 
  NSString *item = [myArray5 objectAtIndex: chosen];
  label3.text = [NSString stringWithFormat: @"%@", item];
  label3.alpha = 0;
  label3.transform = CGAffineTransformIdentity;
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:1.0];
  label3.alpha = 1;
  label3.transform = CGAffineTransformMakeScale(1, 1);
  [UIView commitAnimations];
  UITouch *touch = [[event allTouches] anyObject];
  if (touch.tapCount == 2) {
  }
}
A: 

You could, of course create an NSMutableArray, which safes the indexes (int chosen) of your randomly chosen questions. You also might want to take a look at the UINavigationController which provides the means to navigate backwards in a series of views. Finally, you should think about encapsulating your question-logic. It seems like you have both the code for your interface and the code for your Questions in one class. Once your app gets bigger, you will run into problems with this approach. Look at the concept of Model-View-Controllers.

Phlibbo
A: 

One possibility would be to pre-compute the indeces of the questions the user will be asked when your app initialises. However this would mean there would be a hardcoded limit on the number of questions you would ask, or perhaps if you reach the end of the array you go back to the start. I'm not familiar with Objective-C so bear with me

const int NUM_QUEsTIONS = 1000; // Arbitrary number
int questions[NUM_QUESTIONS];

init()
{
    for (int i = 0; i < NUM_QUESTIONS; ++i)
    {
        questions[i] = RandomNumber();
    }
}

and then in your event handler you simply increment a counter into this array and ask that question.

For infinite questions you should probably use a doubly-linked list.

James
A: 

Couple of ideas:

Idea 1. Store the ids for the chosen question in an extra array.

Idea 2. Randomize the array, and step through it.

Idea 3. If you want to avoid using a question twice, you could

  • use an index to indicate how many questions are eligible for selection
  • based on that index you create a random integer
  • you display the question that is stored in the array at that random position
  • you move the element at that position to the max index
  • you decrease max index

Sort of like this:

  NSMutableArray *array = [NSMutableArray arrayWithObjects: @"a", @"b", @"c", @"d", @"e", @"f", nil];
 NSInteger maxIndex = [array count]-1;
 do {

  NSInteger randomIndex = arc4random() % (maxIndex+1);

  NSLog(@"%@", [array objectAtIndex:randomIndex]);
  [array exchangeObjectAtIndex:randomIndex withObjectAtIndex:maxIndex];
  maxIndex--;



 } while (maxIndex > -1);
Joseph Tura