简体   繁体   中英

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

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

Try this...

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

Thanks

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.

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM