Reputation: 61
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
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
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
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
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
Reputation: 1493
Try this...
-(void)buttonOnClicked:(UIButton *)sender {
sender.enabled = NO;
}
Thanks
Upvotes: 0