Tuesday August 13th, 2019

Swift App – iOS Calculator Part-2

By Ebubekir Sezer

In the previous article, I wrote the design of the iOS Calculator. In this article, I will try to write the idea of the how calculator work in the iOS Calculator.

Firstly, I opened the project which we created before. In the project, I go ViewController.swift and then i add the operation buttons, number buttons and the result label from MainStoryboard.

@IBOutlet weak var resultLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func operationButtons(_ sender: Any) {
    }
    
    @IBAction func numberButtons(_ sender: Any) {
    }

After this process there is nothing to do in the MainStoryboard. In the calculator, I take 2 number from the user so i should have 2 number variable which names are firstNumber and nextNumber and i should know the if the one of the operation button clicked so i add the boolean variable which name is isMath. Also i should keep the value of the which operation clicked so i create a variable which name is operation keeps the tag of the operation buttons.

    var firstNumber : Double = 0
    var nextNumber : Double = 0
    var isMath : Bool = false
    var operation : Int = 0

After the making initialization of the variables, I made the controls of the operationButtons function. I controlled the value of the resultLabel and the which operation clicked. By using the if-else method, I made the process of the giving value to the firstNumber and nextNumber.

@IBAction func operationButtons(_ sender: UIButton) {
        if resultLabel.text != "" && sender.tag != 11 && sender.tag != 17{
            firstNumber = Double(resultLabel.text!)!
            if sender.tag == 12 {
                //%
                resultLabel.text = "%"
                
            }
            else if sender.tag == 13 {
                // /
                resultLabel.text = "/"
                
            }
            else if sender.tag == 14 {
                // *
                resultLabel.text = "X"
            }
            else if sender.tag == 15 {
                // -
                resultLabel.text = "-"
            }
            else if sender.tag == 16 {
                // +
                resultLabel.text = "+"
            }
            operation = sender.tag
            isMath = true
        }
        else if sender.tag == 11{
            firstNumber = 0
            nextNumber = 0
            operation = 0
            resultLabel.text = ""
        }
        else if sender.tag == 17{
            if operation == 12 {
                resultLabel.text = String(firstNumber / 100)
            }
            else if operation == 13 {
                resultLabel.text = String(firstNumber / nextNumber)
            }
            else if operation == 14 {
                resultLabel.text = String(firstNumber * nextNumber)
            }
            else if operation == 15 {
                resultLabel.text = String(firstNumber - nextNumber)
            }
            else if operation == 16 {
                resultLabel.text = String(firstNumber + nextNumber)
            }
        }
    }

After the operation process, i give the value of the firstNumber and nextNumber in the numberButtons function by checking the value of the isMath. Tag of the number buttons is +1 more the label of the button, That’s why i delete -1 from the tag.

 @IBAction func numberButtons(_ sender: UIButton) {
        if isMath == true {
            resultLabel.text = String(sender.tag - 1)
            nextNumber = Double(resultLabel.text!)!
            isMath = false
        }
        else{
            resultLabel.text = resultLabel.text! + String(sender.tag - 1)
            nextNumber = Double(resultLabel.text!)!
        }
    }

After the all these process, our calculator will work right. You can reach the project by clicking here.

If you have any question you can ask via e-mail or comments.