Reputation: 1943
I am trying to design a interface of an app, and i would only like to allow the user to press a UIButton once to get the result. Is there any way i can lock the button after the button is pressed? And release the lock only when another button is pressed?
Thanks
Upvotes: 4
Views: 5371
Reputation: 81
This is the updated version of James Paolantonio's answer for Swift 4
@IBAction func clicked(_ sender: Any) {
//See all buttons enabled
//Try a loop or manually
(sender as? UIButton)?.isEnabled = false
}
Upvotes: 2
Reputation: 3001
You need to have a reference to the first pressed button.. on the next press you can enable the old one and disable the new one
Button *disabledButton;
- (IBAction)clicked:(id)sender {
if (disabledButton)
disabledButton.enabled = YES;
disabledButton = ((UIButton *)sender);
disabledButton.enabled = NO;
}
Upvotes: 0
Reputation: 2194
You can set the button to be disabled once it is clicked:
- (IBAction)clicked:(id)sender {
//See all buttons enabled
//Try a loop or manually
((UIButton *)sender).enabled = NO;
}
Upvotes: 10
Reputation: 51
Just disable the Uibutton when your selector method for the button is called. as in [aButton setEnabled:False];, and when the user taps the other button reenable the first one and disable the second one as in [bButton setEnabled:False] and [aButton setEnabled:True],
hope it helps.
Upvotes: 1
Reputation: 5181
Of course you can. Just use the property enabled
of the UIButton. When the user presses it, set enabled to NO: [myButton setEnabled:NO];
, and set YES
when you need enable it again later.
Upvotes: 2