iOS 入门 —— TipCalculator app

第二弹~
以前我作的Hello World app,好多人看了都以为太简单了。。但毕竟是入门项目,我以为先放一个功能就够了。
千里之行,始于足下。一步一个脚印,相信咱们都能成长的很快的。swift

此次,作的是小费计算的app。咱们都知道,在美国,在哪吃个饭都要付小费,可是发票上不必定都有具体的数值,而只是标了三个百分比选项,15%,18%,20%。而每次都要打开手机上的Calculator我的以为有些麻烦,因此,不如直接开发一个app来计算吧。每次吃完饭内心都有个数吧。app

基本功能很是,很是之简单。。主要仍是熟悉iOS的组件吧ide

  • 输入消费总额函数

  • 输入小费百分比测试

  • 获得总价spa

过程:

  • 建立projectcode

  • 在main storyboard添加组件orm

    • Labelip

    • TextField开发

    • Button

clipboard.png

  • 为组件写相应函数

    • TextField1: 消费总额

    • TextField2: 小费比例

    • Label: 显示总价

    • Button: 点击计算小费并赋予总价Label.text

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var txtBill: UITextField!
    
    @IBOutlet weak var txtTip: UITextField!
    
    @IBOutlet weak var lblTipAmount: UILabel!
    
    @IBAction func CalculateTip(_ sender: Any) {
        let billAmount = (Double)(txtBill.text!)
        let pctTip = (Double)(txtTip.text!)
        let tipAmount = billAmount! * pctTip!/100
        let tipString = String(format:"%.2f",tipAmount)
        lblTipAmount.text = "$" + tipString
    }
    
    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.
    }


}
  • 测试

完成效果图

clipboard.png

Reference

App school for Xcode and iOS 10 Development Free

Copyright © 2017 zhiwei xu. All rights reserved.
相关文章
相关标签/搜索