Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling and Enabling a button on event iOS [duplicate]

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

like image 848
peterbonar Avatar asked Dec 14 '15 15:12

peterbonar


2 Answers

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
like image 140
dasdom Avatar answered Oct 28 '22 11:10

dasdom


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;
like image 41
TwoStraws Avatar answered Oct 28 '22 11:10

TwoStraws