0

I'm working on a project, I have a requirement of disabling the UIButton after button is clicked or pressed.

TIA

KSR
  • 1,699
  • 15
  • 22
Abhimanyu
  • 61
  • 3
  • 10

5 Answers5

2

On your Button action method set enabled of Button to NO.

- (IBAction)buttonTapped:(UIButton*)sender {    
    sender.enabled = NO; // With disabled style on button 
    //If you doesn't want disabled style on button use this.
    sender.userInteractionEnabled = NO;
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • it directly disable button, but my requirement is after it's first click the button should be disabled – Abhimanyu Aug 30 '16 at 11:36
  • 1
    Thats why i written on button action, check my answer properly. Button action method only call when you perform its specific action. – Nirav D Aug 30 '16 at 11:36
0

Try this...

-(void)buttonOnClicked:(UIButton *)sender {
    sender.enabled = NO;
}

Thanks

Sailendra
  • 1,318
  • 14
  • 29
0

You can do that by two ways:

Button.userInteractionEnabled = NO; 

or use:

Button.enabled = NO;

Like,

- (IBAction)btnClicked:(UIButton*)sender {    
    sender.enabled = NO;
    //OR
    //sender.userInteractionEnabled = NO;
}

It will be good to go with setEnabled:NO because, it is proper way to do it and it will also update the UI.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0

You can set false for enabled property like below.

Objective-C

- (IBAction)btnNextClicked:(UIButton *)sender {    
    sender.enabled = NO; 
}

Swift

@IBAction func btnNextClicked(sender: UIButton) {
    sender.enabled = false
}
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
0

After you first click or when you try to click second time it does not enable.I set the condition.I save the first click value into NSUserdefault.I tried sample and it works fine.

- (IBAction)actionGoPrevious:(id)sender
{
   NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   NSInteger btnFirstClick = [userDefaults integerForKey:@"firstClick"];
   if(btnFirstClick == 1){
    btnBack.userInteractionEnabled = NO;
        //OR
    btnBack.enabled = NO;
  }
  else{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:1 forKey:@"firstClick"];
    [defaults synchronize];
    [self.navigationController popToRootViewControllerAnimated:YES];
  }
}
user3182143
  • 9,459
  • 3
  • 32
  • 39