按下按钮后禁用UIButton

时间:2016-08-30 11:29:42

标签: ios objective-c swift uibutton event-handling

我正在开发一个项目,我需要在单击或按下按钮后禁用ans

TIA

5 个答案:

答案 0 :(得分:2)

在您的Button操作方法集enabled ButtonNO

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

答案 1 :(得分:0)

试试这个......

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

由于

答案 2 :(得分:0)

您可以通过两种方式实现这一目标:

Button.userInteractionEnabled = NO; 

或使用:

Button.enabled = NO;

像,

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

setEnabled:NO一起使用会很好,因为这是正确的方法,它也会更新用户界面。

答案 3 :(得分:0)

您可以为启用的属性设置false,如下所示。

<强>目标C

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

Swift

@IBAction func btnNextClicked(sender: UIButton) {
    sender.enabled = false
}

答案 4 :(得分:0)

首次点击后或当您尝试第二次点击时它没有启用。我设置了条件。我将第一个点击值保存到NSUserdefault.I尝试了样本,它工作正常。

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