Hi, this is another basic app, that seems to be a must for every beginner.
Again we are implementing the use of a slider and some basic math operations.
Also this will help you understand how to put everything you need to reuse in a function.
It makes things much more easy to use, later change and presentable.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
// // ViewController.swift // TipCalclator2 // // Created by Razvigor Andreev on 10/12/14. // Copyright (c) 2014 Razvigor Andreev. All rights reserved. // import UIKit class ViewController: UIViewController { // Labels @IBOutlet weak var billAmountLabel: UILabel! @IBOutlet weak var tipLabel: UILabel! @IBOutlet weak var totalPlusTipLabel: UILabel! @IBOutlet weak var tipPerPersonLabel: UILabel! @IBOutlet weak var totalPerPersonLabel: UILabel! // Outputs @IBOutlet weak var tipPercentageLabel: UILabel! @IBOutlet weak var personsLabel: UILabel! @IBOutlet weak var tipOutlet: UITextField! @IBOutlet weak var total: UITextField! @IBOutlet weak var tipPerPerson: UITextField! @IBOutlet weak var totalPerPerson: UITextField! // Input @IBOutlet weak var billAmountField: UITextField! // variables var amountDouble = Double(50) var sliderValue = 18 var tipPerc = 0.18 var tip = Double(9) var myTip = Double(9) var persons = Int(1) // Sliders @IBOutlet weak var tipSlider: UISlider! @IBOutlet weak var personsSlider: UISlider! 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. } @IBAction func tipSliderChanged(sender: UISlider) { println("tip adjusted") sliderValue = Int(sender.value) tipPerc = Double(sliderValue) * 0.01 refresh() } @IBAction func personsSlider(sender: UISlider) { println("person adjusted") persons = Int(sender.value) personsLabel.text = "(persons)" refresh() } func refresh () { println("refresh requested") let pctFormatter = NSNumberFormatter() pctFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle amountDouble = (billAmountField.text as NSString).doubleValue tip = amountDouble * tipPerc tipPercentageLabel.text = "(Int(sliderValue)) %" tipOutlet.text = pctFormatter.stringFromNumber(tip) var result = Double(amountDouble)+Double(tip) total.text = pctFormatter.stringFromNumber(result) var personsDouble = Double(persons) tipPerPerson.text = pctFormatter.stringFromNumber(tip/personsDouble) myTip = Double(tip/personsDouble) totalPerPerson.text = pctFormatter.stringFromNumber(result / personsDouble) } @IBAction func billAmountChanged(sender: AnyObject) { println("amount changed") refresh() } } |