Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling UIButton not working

The solutions i'm finding on here use .enabled which is old, rather than .isEnabled.

So i'm currenting trying to just disable the functionality/clickability of buttons if a certain condition is (or isn't) met. So before it all I disable them all if a condition isn't met then if there on after (dynamically) it is met then it should theoretically enable. That being said, it is not initially disabling the buttons when I start off with .isEnabled = false.

I know the condition is being met because I have print statements and other tests (like labels being removedfromsuperview yet .isEnabled = false for the buttons isn't working. Anyone encountered said problems or have any solutions?

Code below:

override func viewDidLoad()
{
    super.viewDidLoad()
    trumpMoneyDefault.setValue(50, forKey: "TrumpMoney")
    print("UnoViewController")
    //make all the buttons that shouldn't be clickable unlcickable
    locklvl2.isEnabled = false
    locklvl3.isEnabled = false
    trumplvl2.isEnabled = false
    trumplvl3.isEnabled = false
    lvl2.isEnabled = false
    lvl3.isEnabled = false

    //make level2/3 unclickable by defeault
    //lvl2.isEnabled = false
    //lvl3.isEnabled = false
    //update trumpmoney label depending on if they have enough cash
    //also here check if they have already unlocked all via purchase of unlock all. If so, then skip all this
    if trumpMoneyDefault.value(forKey: "TrumpMoney") != nil
    {
        trumpmoney.text = trumpMoneyDefault.value(forKey: "TrumpMoney") as? String

        //remove locks if they got the money by default.
        let tempTrumpMoneyDefault = trumpMoneyDefault.value(forKey: "TrumpMoney") as! Int
        if tempTrumpMoneyDefault >=  100
        {
            locklvl2.removeFromSuperview()
            moneylvl2.removeFromSuperview()
            trumplvl2.removeFromSuperview()
            lvl2.isEnabled = true
            if tempTrumpMoneyDefault >=  500
            {
                locklvl3.removeFromSuperview()
                moneylvl3.removeFromSuperview()
                trumplvl3.removeFromSuperview()
                lvl3.isEnabled = true
            }
        }
    }
}
like image 242
insta catering Avatar asked Mar 28 '17 15:03

insta catering


2 Answers

Really? It should work.

1.)This is how I disable a button and it's working.

myButton.isEnabled = false;

2.)Desperate way to disable button by disabling the user interaction.

myButton.isUserInteractionEnabled = false; 
like image 160
handiansom Avatar answered Oct 04 '22 05:10

handiansom


.enabled is a previous Swift versions way of determining whether the button is disabled or not.

Swift 3

Use .isEnabled = true or .isEnabled = false depending on what you want to do.

like image 32
Brandon Avatar answered Oct 04 '22 03:10

Brandon