tags:

views:

132

answers:

2

I've managed to get my app to count the number of actions on an IBAction button, and then perform a different task once the number of clicks exceeds 10.

Unfortunately, the Count seems to be increasing by 3 at a time, instead of 1.

Any ideas what I've done wrong here?

- (IBAction) do_button_press:(id)sender {
    static int count = 0;
    count++;
    label.text = [NSString stringWithFormat:@"%d\n", count];

    if (count++ > 10) {
        label.text = @"done";
    }
}
+3  A: 

Shouldn't your if statement look like:

if (count > 10)

rather than:

if (count++ > 10)

?

Using your original code, the first time count is used, its value is 1, then incremented to 2 (by the count++ in the if statement), then incremented to 3 (by the count++ in line 3)

ryanprayogo
bingo! thanks man! :)
Simon Hume
That's certainly part of the problem.
Andrew Medico
A: 

I can see two "count++" in your method, so you're incrementing count at least twice.

 - (IBAction) do_button_press:(id)sender {
     static int count = 0;
     label.text = [NSString stringWithFormat:@"%d\n", count];
     if (count++ > 10) {
         label.text = @"done";
     }
 }

Just remove the first count++.

pfandrade
thanks for the answer, got it all sorted. just a late night i think!
Simon Hume