title: UIAlertView showing twice
body: <p>I have an application that, in part, loops through the contents of an NSSet and displays a UIAlertView for each item found in the set. When there is only one item in the set, the UIAlertView behaves itself properly. However, if there's more than one, the first view flashes up (normally with the contents of the last item in the set) and then disappears without any user intervention. The first item in the NSSet will then display and wait for a response, before displaying the next item in the NSSet and so on. </p>
  
  <p>It is the same experience as is being described in this unresolved question: <a href="https://stackoverflow.com/questions/4483410/iphone-uialertview-called-twice-in-a-custom-function-ibaction">IPHONE: UIAlertView called twice in a custom function/IBAction</a></p>
  
  <p>Here's the code:</p>
  
  <pre><code>#import "CalcViewController.h"
  
  @interface CalcViewController()
  @property (nonatomic) int variablesCount;
  @property (nonatomic, strong) NSMutableDictionary *variablesSet;
  @end
  
  @implementation CalcViewController
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      self.variablesSet = [[NSMutableDictionary alloc] init];
  }
  
  
  - (IBAction)variablePressed:(UIButton *)sender
  {
      [[self calcModel] setVariableAsOperand:sender.titleLabel.text];
      self.expressionDisplay.text = [[self calcModel] descriptionOfExpression:self.calcModel.expression];
  }
  
  - (IBAction)solveExpressionPressed:(UIButton *)sender {
      self.variablesCount = 0;
      [self.variablesSet removeAllObjects];
  
      NSSet *variablesCurrentlyInExpression = [[NSSet alloc] initWithSet:[CalcModel variablesInExpression:self.calcModel.expression]];
      self.variablesCount = [variablesCurrentlyInExpression count];
  
      if (variablesCurrentlyInExpression){
          for (NSString *item in variablesCurrentlyInExpression) {
              UIAlertView *alertDialog;
              alertDialog = [[UIAlertView alloc] initWithTitle:@"Enter value for variable"
                                                  message:item
                                                  delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
  
              alertDialog.alertViewStyle=UIAlertViewStylePlainTextInput;
              UITextField * alertTextField = [alertDialog textFieldAtIndex:0];
              alertTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
              [alertDialog show];
          }
  
      }
  }
  
  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == 0){
          if ([[alertView textFieldAtIndex:0] text]){         
              self.variablesSet[alertView.message] = [[alertView textFieldAtIndex:0] text];
          }
      }
  
      if ([self.variablesSet count] == self.variablesCount){
          NSLog(@"time to solve");
          [[self calcDisplay] setText:[NSString stringWithFormat:@"%g", [CalcModel evaluateExpression:self.calcModel.expression usingVariableValues:self.variablesSet]]];
      }
  }
  </code></pre>
  
  <p>I've checked the IBActions behind the button that triggers the solveExpressionPressed method and that is the only one that exists. I've also placed some logging before the [alertDialog show]; line and it is only called twice when the variablesCurrentlyInExpression NSSet contains two values, yet the UIAlertView appears three times (flashing up once).</p>
  
  <p>Finally, i've tried it without the following code:</p>
  
  <pre><code>            UITextField * alertTextField = [alertDialog textFieldAtIndex:0];
              alertTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
  </code></pre>
  
  <p>and the problem still occurs, so I don't think it's that.</p>
  
  <p>I've been stuck on this a while and haven't figured it out (hence the post!!), so any help would be greatly appreciated.</p>
  
  <p>Thanks</p>