I have a stopclock app and would like for the startCountButton
button to be disabled when it is initially pressed and then once the stopCountButton
button is pressed for it to be enabled again so that the start button can only be pressed once. Here is my code
- (IBAction)startCount:(UIButton*)sender {
countInt = 0;
self.label.text = [NSString stringWithFormat:@"%i", countInt];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countTimer) userInfo:nil repeats:YES];
}
- (IBAction)stopCount:(UIButton*)sender {
countInt = 0;
[timer invalidate];
}
-(void)countTimer {
countInt += 1;
self.label.text = [NSString stringWithFormat:@"%i", countInt];
}
Any help on what I would need to add? I am not looking to change text, just disabling it
First, you need a reference to the button. Then, add the following code to startCount:
:
((UIButton *)sender).enabled = NO
and in stopCount:
add:
startCountButton.enabled = YES
Both startCount:
and stopCount:
accept a UIButton
as their parameter, but I'm confused as to how the second one gets called.
If startCount:
is called by the button you want to disable, you can just write this:
sender.enabled = NO;
But stopCount:
is tricky, because clearly it can't be called by the button as it was disabled a moment ago. If stopCount:
is being called from a different button (as I guess it must be) then you must store a reference to the first button in order to re-enable it. You can then do:
self.disabledButton.enabled = YES;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With