Search code examples
swiftcalculatorpointrpn

Swift enable point - make a point be clickable again


I am a beginner and I am building a RPN calculator. I have disabled the point key (not clickable anymore) if the digit situated in the display has already a point. How can I enable it again after having pressed the enter key? Because now, it stays disabled even if I enter another digit in the display. So if I clicked on the point key once for the first digit, I cannot add a point for the second digit of my operation.

I have that code for to add a point to a digit:

    @IBAction func floatingPoint(sender: UIButton) {
    labelDisplay.text = labelDisplay.text! + "."
    sender.enabled = false //not clickable if the digit as already a point  
}

I have that code for enter:

var enterPressed = false
@IBAction func Enter() {
    userHasStartedTyping = false
    self.calcEngine!.operandStack.append(displayValue)
    print("Operand Stack on engine = \(self.calcEngine!.operandStack)")
}

I have that code for the operation:

    @IBAction func operation(sender: UIButton) {
    let operation = sender.currentTitle!
    if userHasStartedTyping { 
        Enter()  
    }
    self.displayValue = (self.calcEngine?.operate(operation))!
    Enter() //


}

Solution

  • You will have to add an @IBOutlet in your class, so you can access this button from various parts of your code, not just it's action method. For example, add this to the top of your view controller:

    @IBOutlet var pointButton:UIButton!
    

    Remember to connect the button to this outlet on your storyboard.

    With that, you can re-enable the point button in your Enter() method for example. Like this:

    pointButton:UIButton.enabled = true