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
            }
        }
    }
}
insta catering
  • 151
  • 2
  • 12
  • Could you show your actual code of how you are trying to disable your buttons? – Michael Dautermann Mar 28 '17 at 15:43
  • @MichaelDautermann Just edited it in! – insta catering Mar 28 '17 at 15:46
  • @instacatering are those references to buttons from Storyboard/xibs? i.e, how is `locklvl2` declared? – Edgar Mar 28 '17 at 15:48
  • Without a broader context it's hard to say, but should you be doing this in *viewDidLoad*? –  Mar 28 '17 at 15:48
  • 1
    @Edgar 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. – insta catering Mar 28 '17 at 15:51
  • @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? – Edgar Mar 28 '17 at 16:18
  • If it's IBOutlet, have you tried setting disabled in the storyboard? If so, does it work when implemented that way? – dst3p Mar 28 '17 at 16:22
  • @Edgar No they are strong. the removefromsuperview isn't on the buttons i'm having issues with. That part is working literally perfectly fine! – insta catering Mar 28 '17 at 23:48

4 Answers4

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; 
handiansom
  • 783
  • 11
  • 27
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.

Brandon
  • 200
  • 1
  • 9
  • 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. – insta catering Mar 28 '17 at 15:47
  • My bad - completely misread. I would take the code outside of the `ViewDidLoad`, and try putting it inside a `ViewDidAppear` method. – Brandon Mar 28 '17 at 15:51
  • I've been able to call many other UIButton functions inside of the viewdidload so I don't see difference in this case :/ – insta catering Mar 28 '17 at 15:55
  • Have you tried putting it inside the `viewWillAppear` method? - I don't think that it should be called in the `ViewDidLoad` – Brandon Mar 28 '17 at 15:56
  • that didn't change a thing. I tried, didn't help :/ – insta catering Mar 30 '17 at 00:57
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.

Aziz
  • 38
  • 1
  • 7
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

Carlos M.
  • 147
  • 9