Tip Calculator in Swift Programming

How to write a program to create Tip Calculator in Swift Programming


//
//  ViewController.swift
//  Tip Calculator
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    //Outlets
    @IBOutlet weak var tenPercent: UIButton!
    @IBOutlet weak var fifteenPercent: UIButton!
    @IBOutlet weak var twentyPercent: UIButton!
    @IBOutlet weak var twentyFivePercent: UIButton!
    @IBOutlet weak var billInput: UITextField!
    @IBOutlet weak var finalTipAmount: UILabel!
    @IBOutlet weak var splitDisplay: UILabel!
    @IBOutlet weak var personDisplay: UILabel!
    @IBOutlet weak var downButtonOutlet: UIButton!
    
    @IBAction func billInputAction(sender: UITextField) {
        billInput.resignFirstResponder()
        billAmount = (Double)(billInput.text!)!
    }
    @IBAction func viewTapped(sender : AnyObject) {
        billInput.resignFirstResponder()
        billAmount = (Double)(billInput.text!)!
        
    }
    
    
    @IBAction func tenPercent(sender: UIButton) {
        resetSelectedPercentage(10)
    }
    @IBAction func fifteenPercent(sender: AnyObject) {
        resetSelectedPercentage(15)
    }
    @IBAction func twentyPercent(sender: UIButton) {
        resetSelectedPercentage(20)
    }
    @IBAction func twentyFivePercent(sender: AnyObject) {
        resetSelectedPercentage(25)
    }
    
    @IBAction func calculateButton(sender: UIButton) {
        let tempResult = (billAmount * gratuityPercent) / (Double)(peopleToSplit)
        finalTipAmount.text = "$" + (NSString(format: "%.2f", tempResult) as String)
    }
    @IBAction func upButton(sender: AnyObject) {
        peopleToSplit++
        splitDisplay.text = (String)(peopleToSplit)
        if(peopleToSplit > 1){
            personDisplay.text = "People"
            downButtonOutlet.enabled = true
        }
    }
    @IBAction func downButton(sender: AnyObject) {
        peopleToSplit--
        splitDisplay.text = (String)(peopleToSplit)
        if(peopleToSplit == 1){
            personDisplay.text = "Person"
            downButtonOutlet.enabled = false
        }
    }
    
    
    
    //Variables
    var gratuityPercent = 0.0
    var billAmount = 0.0
    var peopleToSplit = 1
    
    
 
    //Functions
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        billAmount = (Double)(billInput.text!)!
        billInput.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
        billAmount = (Double)(billInput.text!)!
    }
    
    
   
    func resetSelectedPercentage(percentage:Int){
        tenPercent.setImage(UIImage(named: "10_unselected_image"), forState: UIControlState.Normal)
        fifteenPercent.setImage(UIImage(named: "15_unselected_image"), forState: UIControlState.Normal)
        twentyPercent.setImage(UIImage(named: "20_unselected_image"), forState: UIControlState.Normal)
        twentyFivePercent.setImage(UIImage(named: "25_unselected_image"), forState: UIControlState.Normal)
        switch percentage {
        case 10:
            gratuityPercent = 0.1
            tenPercent.setImage(UIImage(named: "10_selected_image"), forState: UIControlState.Normal)
        case 15:
            gratuityPercent = 0.15
            fifteenPercent.setImage(UIImage(named: "15_selected_image"), forState: UIControlState.Normal)
        case 20:
            gratuityPercent = 0.2
            twentyPercent.setImage(UIImage(named: "20_selected_image"), forState: UIControlState.Normal)
        case 25:
            gratuityPercent = 0.25
            twentyFivePercent.setImage(UIImage(named: "25_selected_image"), forState: UIControlState.Normal)
        default:
            gratuityPercent = 0.0
        }
        
    }


}


Learn More :