0
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSLog(@"buttonindex: %i", buttonIndex);
    NSLog(@"tag: %i", alertView.tag);
    if ((alertView.tag <= 3) && (alertView.tag >= 1)) {

    } //Between 1 and 3
    else if (alertView.tag = 8) {

        }
    else if (alertView.tag = 10) {
        NSLog(@"Test");

    }
}

For some reason, even though i'm using alertView with tag 10, and it's returning "Tag: 10" on the NSLog, it isn't showing the "Test" log, or processing any code from within those last brackets. And yet alertView tag 8 is working fine.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Andrew
  • 15,935
  • 28
  • 121
  • 203

1 Answers1

8

Be careful, it should be == in your if statements. Apart from that, how do you assign tags to the alertviews?

phi
  • 10,634
  • 6
  • 53
  • 88
  • 2
    just to add to this... alertview tag 8 is working fine because using a single `=` in your if statements means that your code reads "if the tag ISN'T 2 then set the alertView.tag to 8 (which will return true because its completed successfully) and then it runs the bit for =8". So as long as the first if query isn't true then the second one WILL always be true. – Thomas Clayson Feb 08 '11 at 16:42
  • Almost. The assignment evaluates to true because the value of the left-hand side of the expression is non-zero after the assignment. – jlehr Feb 08 '11 at 16:47
  • lol... yes, I understand - its the same reason why `echo $var = "hello";` works in php I'm guessing. – Thomas Clayson Feb 08 '11 at 16:59
  • I see! And can't believe i missed that. Thanks. – Andrew Feb 08 '11 at 17:02
  • 1
    In languages that allow assignment it is recommended to put the literals first `if(8 = alertView.tag)` would have nicely given you an error – Joe Feb 08 '11 at 20:21