4

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

Clarence
  • 1,951
  • 2
  • 34
  • 49

5 Answers5

10

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;
}
James Paolantonio
  • 2,194
  • 1
  • 15
  • 32
2

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.

Natan R.
  • 5,141
  • 1
  • 31
  • 48
2

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
}
JipZipJib
  • 81
  • 3
  • 5
1

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.

c4code
  • 51
  • 2
0

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;
 }
lukaswelte
  • 2,951
  • 1
  • 23
  • 45