iOS Tutorials: Calculation App using Textfield

OVERVIEW

The project here focus on creating an app in which you are able to do calculation according to your need. An additional skills being used here is to use the number typed on a TextField and let it do it’s calculation.

You should be able to by now identify the object by looking at it. If not, some of the objects being used here are TextFields, Buttons and Label.

The goal here is to be able to input two different number and click on any of those button and the symbol will indicate the mathematical calculation being done.

Make sure that you create an IBOutlet as shown below.

class ViewController: UIViewController {

    @IBOutlet weak var firstNumberLabel: UITextField!
    @IBOutlet weak var secondNumberLabel: UITextField!
    @IBOutlet weak var totalNumberLabel: UITextField!
    @IBOutlet weak var symbolLabel: UILabel!

Now we will first create the adding mathematical functionality using the IBAction of the button.

    @IBAction func addButton(_ sender: Any) {
        let firstNumber = firstNumberLabel.text!
        let firstValue = (firstNumber as NSString).integerValue
        let secondNumber = secondNumberLabel.text!
        let secondValue = (secondNumber as NSString).integerValue
        let totalValue = firstValue + secondValue
        symbolLabel.text = "+"
        totalNumberLabel.text = "\(totalValue)"
        
    }

And then, subtraction functionality

    @IBAction func subtractButton(_ sender: Any) {
        let firstNumber = firstNumberLabel.text!
        let firstValue = (firstNumber as NSString).integerValue
        let secondNumber = secondNumberLabel.text!
        let secondValue = (secondNumber as NSString).integerValue
        let totalValue = firstValue - secondValue
        symbolLabel.text = "-"
        totalNumberLabel.text = "\(totalValue)"
    }

And then, division functionality

    @IBAction func divideButton(_ sender: Any) {
        let firstNumber = firstNumberLabel.text!
        let firstValue = (firstNumber as NSString).integerValue
        let secondNumber = secondNumberLabel.text!
        let secondValue = (secondNumber as NSString).integerValue
        let totalValue = firstValue / secondValue
        symbolLabel.text = "/"
        totalNumberLabel.text = "\(totalValue)"
    }

Lastly, the multiplication functionality

    @IBAction func multiplyButton(_ sender: Any) {
        let firstNumber = firstNumberLabel.text!
        let firstValue = (firstNumber as NSString).integerValue
        let secondNumber = secondNumberLabel.text!
        let secondValue = (secondNumber as NSString).integerValue
        let totalValue = firstValue * secondValue
        symbolLabel.text = "*"
        totalNumberLabel.text = "\(totalValue)"
    }

When you are done, the app will look like below.

 

 

The code can be downloaded in Github

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

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

*
*