title: Switch statement using a selectedSegmentIndex
body: <p>How do to all,</p>
  
  <p>This is the first time posting on the site, so i presume i have not followed all the
  formats and protocols correctly.</p>
  
  <p>My app has a UIView containing a list of questions, and a UISegmentedControl called "question_btn" for each question. The UISegmentedControl has 2 segments, the first for a NO answer, the 2nd for a YES answer, corresponding to each question.</p>
  
  <p>Each "question_btn" individually calls -(void) processQuestions{}, but when i click on each "question_btn" only the last one is processed properly. All previous "question_btns" call the processQuestions method, but do not implement its code.</p>
  
  <p>Im my problem with the UISegmentControl, or the switch statement, or both.</p>
  
  <p>Any suggestions would be greatly appreciated.</p>
  
  <p>The button is defined as follows</p>
  
  <pre><code> question_btn  = [[[UISegmentedControl alloc] initWithFrame:CGRectMake(QUESTION_BUTTON_XPOS, questionsButton_YPos, 80, 20)] autorelease]; 
   [question_btn insertSegmentWithTitle:@"No" atIndex:0 animated:NO];
   [question_btn insertSegmentWithTitle:@"Yes" atIndex:1 animated:NO];
   [question_btn setEnabled:YES forSegmentAtIndex:0];
   [question_btn setEnabled:YES forSegmentAtIndex:1];
   question_btn.segmentedControlStyle = UISegmentedControlStyleBar;
   question_btn.tintColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:1.0];
   [question_btn addTarget:self action:@selector(processQuestions) forControlEvents:UIControlEventValueChanged]; 
  </code></pre>
  
  <p>Below is the processQuestions</p>
  
  <pre><code>- (void) processQuestions
  {
   //NSLog(@"QuestionsView processQuestions: Called");
   //Process the questions i.e. determine whether yes or no was the answer
  
       answersArray = [[NSMutableArray alloc] init]; 
  
       int selectedIndex; 
  
       selectedIndex = [question_btn selectedSegmentIndex];
  
       switch (selectedIndex)
       {
           case 0:
  
               NSLog(@"QuestionsView processQuestions: No answered");    
  
               break;
  
           case 1:    
  
               NSLog(@"QuestionsView processQuestions: Yes answered");    
  
               break;  
       } 
  
       [answersArray addObject:[NSNumber numberWithInt:selectedIndex]];
  
       NSLog(@"QuestionsView processQuestions: answersArray contains: %@",  answersArray);
  }
  </code></pre>
  
  <p>Here is how i got the code to iterate properly and get the question_btn selected index value. It was just a matter of creating a mutable array to hold the question_btn objects,
  and then set the selected index value for each button using a for loop.
  The buttonsArray is set in my createView method.</p>
  
  <pre><code>buttonsArray = [[NSMutableArray alloc] init];
  
  - (void) processQuestions
  </code></pre>
  
  <p>{
      //NSLog(@"QuestionsView processQuestions: Called"); </p>
  
  <pre><code>answersArray = [[NSMutableArray alloc] init];           
  
  int selectedIndex;
  
  for (int i = 0; i &lt; numQuestions; i ++)
  {   
      question_btn = (UISegmentedControl*)[buttonsArray objectAtIndex:i];     
  
      selectedIndex = [question_btn selectedSegmentIndex];
  
      switch (selectedIndex)
      {
          case 0:
  
              NSLog(@"QuestionsView processQuestions: No answered for question: %d", i + 1);              
  
              break;
  
          case 1:             
  
              NSLog(@"QuestionsView processQuestions: Yes answered for question: %d", i + 1);     
  
  
              break;      
      }   
  
      [answersArray addObject:[NSNumber numberWithInt:selectedIndex]];
  }
      NSLog(@"QuestionsView processQuestions: answersArray contains: %@", answersArray);
  </code></pre>
  
  <p>}</p>