Thor
Thor

Reputation: 10058

Trying to build a calculator using Swift, basic operator buttons disappeared from main view controller, can't get it back

I'm attempting the Swift beginner's project of building a calculator. I'm currently trying to add basic operators to the calculator.

enter image description here

However, I don't know what I have done, but suddenly, all the basic operators disappeared from the main vie controller

enter image description here

enter image description here

But the little + sign to the left of @IBAction func operator() still indicates the presence of basic operator, but it is no where to be found.

But when I run the program in Simulator, the basic operator reappears

enter image description here

Code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var display: UILabel!

    var userIsInTheMiddleOfTypingANumber = false

    @IBAction func appendDigit(sender: UIButton) {
        let digit = sender.currentTitle!
        if userIsInTheMiddleOfTypingANumber {
            display.text = display.text! + digit
        } else {
            display.text = digit
            userIsInTheMiddleOfTypingANumber = true
        }
    }

    @IBAction func operate(sender: UIButton) {
        let operation = sender.currentTitle!
        if userIsInTheMiddleOfTypingANumber {
            enter()
        }
        switch operation {
        case "×":performOperation {$0 * $1}
        case "÷":performOperation {$1 / $0}
        case "+":performOperation {$0 + $1}
        case "−":performOperation {$1 - $0}
        case "√":performOperation {sqrt($0)}
        default: break
        }

    }

    func performOperation(operation: (Double, Double) -> Double){
        if operandStack.count >= 2{
            displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
            enter()
        }
    }

    private func performOperation(operation: Double -> Double){
        if operandStack.count >= 1{
            displayValue = operation(operandStack.removeLast())
            enter()
        }
    }

    var operandStack = Array<Double>()

    @IBAction func enter() {
        userIsInTheMiddleOfTypingANumber = false
        operandStack.append(displayValue)
        print("operantStack = \(operandStack)")
    }

    var displayValue: Double {
        get {
            return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
        }
        set {
            display.text = "\(newValue)"
            userIsInTheMiddleOfTypingANumber = false
        }
    }

}

Upvotes: 0

Views: 254

Answers (2)

sschale
sschale

Reputation: 5188

Those buttons are disabled for the current size class that you have selected. If you go over to the Utilities bar (on the right) and select the Attributes Tab (the one in the middle), scroll all the way to the bottom, make sure the first Installed box is checked for them. Have fun with the Stanford series. See attached

Upvotes: 1

O-mkar
O-mkar

Reputation: 5658

it's a constrain problem you are facing

just click on Resolve Autolayout issue under all view in container -> Click Update Frames and click update Constrain

this will solve your issue

enter image description here

Upvotes: 1

Related Questions