Disabling UIButton not working
Asked Answered
G

4

7

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
            }
        }
    }
}
Gustav answered 28/3, 2017 at 15:41 Comment(8)
Could you show your actual code of how you are trying to disable your buttons?Stammer
@MichaelDautermann Just edited it in!Gustav
@instacatering are those references to buttons from Storyboard/xibs? i.e, how is locklvl2 declared?Dissoluble
Without a broader context it's hard to say, but should you be doing this in viewDidLoad?Boser
@Dissoluble Yes IBOutlet drag and drop. @ dfd I don't think it would cause any issues as I can call UILabel functions and other direct UIButton functions in it.Gustav
@instacatering you are calling removeFromSuperview() on a view owned by the view controller's view from the Storyboard via IBOutlet. I believe once you call removeFromSuperview() that button becomes dealloc'ed since nothing is holding it strongly (your IBOutlet references are probably weak right?Dissoluble
If it's IBOutlet, have you tried setting disabled in the storyboard? If so, does it work when implemented that way?Scutiform
@Dissoluble No they are strong. the removefromsuperview isn't on the buttons i'm having issues with. That part is working literally perfectly fine!Gustav
B
5

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; 
Bunch answered 28/3, 2017 at 15:46 Comment(1)
.isUserInteractionEnabled = false/true worked for me :DGustav
W
3

.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.

Westernmost answered 28/3, 2017 at 15:46 Comment(5)
That's what I stated in the question haha. I was basically saying that I couldn't use previous stack questions/answers because they used old swift.Gustav
My bad - completely misread. I would take the code outside of the ViewDidLoad, and try putting it inside a ViewDidAppear method.Westernmost
I've been able to call many other UIButton functions inside of the viewdidload so I don't see difference in this case :/Gustav
Have you tried putting it inside the viewWillAppear method? - I don't think that it should be called in the ViewDidLoadWesternmost
that didn't change a thing. I tried, didn't help :/Gustav
R
1

The question is old but I couldn't find any solution to my bug, so here it is. For anyone who uses Storyboard, be sure that UIButtons don't have multiple @IBOutlet and there is no intersection among them. That could happen if you were lazy and duplicated the button.

Ruthenious answered 23/2, 2019 at 11:55 Comment(0)
S
0

I had the same issue with a UITextField, no matter what I did I couldn't disable it.

The solution that worked for me was to clean the build folder.

I'm using Xcode 10.1 and Swift 4.2

Salema answered 22/3, 2019 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.