Clarence
Clarence

Reputation: 1943

Only allow users to press UIButton once

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

Answers (5)

JipZipJib
JipZipJib

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

lukaswelte
lukaswelte

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

James Paolantonio
James Paolantonio

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

c4code
c4code

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

Natan R.
Natan R.

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

Related Questions