Disabling and enabling a button in an iOS event

I have a stopclock application and would like the startCountButton button startCountButton be disabled when it was initially pressed, and then when the stopCountButton button was pressed so that it was turned on again so that the start button could 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 will need to add? I don’t want to change the text just by turning it off

+5
source share
3 answers

First you need a link to the button. Then add the following code to startCount: ::

 ((UIButton *)sender).enabled = NO 

and in stopCount: add:

 startCountButton.enabled = YES 
+5
source

Both startCount: and stopCount: take UIButton as their parameter, but I'm confused about how the second is called.

If startCount: is called by the button you want to disable, you can simply write this:

 sender.enabled = NO; 

But stopCount: difficult, because it is clear that the button cannot be called, since it was disabled minutes ago. If stopCount: is called from another button (I suppose it should be), you must save the link to the first button to enable it again. Then you can:

 self.disabledButton.enabled = YES; 
+1
source

With enable decency, you can enable / disable the button:

 startCountButton.enabled = NO 
0
source

Source: https://habr.com/ru/post/1238143/


All Articles