iPhone COUNT going up in increments of 3
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";
}
}
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)
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++.
精彩评论