Abhimanyu
Abhimanyu

Reputation: 61

Disable UIButton after button is pressed

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

TIA

Upvotes: 0

Views: 2845

Answers (5)

user3182143
user3182143

Reputation: 9589

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];
  }
}

Upvotes: 0

Narayana Rao Routhu
Narayana Rao Routhu

Reputation: 6323

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
}

Upvotes: 0

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5435

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.

Upvotes: 0

Nirav D
Nirav D

Reputation: 72410

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;
}

Upvotes: 2

SKD
SKD

Reputation: 1493

Try this...

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

Thanks

Upvotes: 0

Related Questions