Press "Enter" to skip to content

Swift App – iOS Calculator Part-2

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.

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.

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.

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.

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.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *