<전체 코드>
//
// ViewController.swift
// BMICalc
//
// Created by 이예은 on 2022/12/26.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var heightField: UITextField!
@IBOutlet weak var weightField: UITextField!
@IBOutlet weak var bmiField: UITextField!
let numberFormatter:NumberFormatter = {
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.minimumFractionDigits = 0
nf.maximumFractionDigits = 3
return nf
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func bmiClac(_ sender: Any) {
if let heightText = heightField.text, let height = Double(heightText), let weightText = weightField.text, let weight = Double(weightText){
let bmi = weight / (height/100)*(height/200)
bmiField.text = numberFormatter.string(from: NSNumber(value: bmi))
}
view.endEditing(true)
}
@IBAction func textFieldFinishEdit(_ sender: UITextField) {
sender.resignFirstResponder()
}
@IBAction func textEndEditing(_ sender: Any) {
view.endEditing(true)
}
}
반응형
'iOS > Swift' 카테고리의 다른 글
SCC 스터디 [Level1] - Swift문법 시작하기 (0) | 2023.06.20 |
---|---|
Swift - 섭씨를 화씨로 변환하는 프로그램 만들기 (0) | 2022.12.28 |
Swift 문법 - 배열 Array Type (0) | 2022.12.22 |
Swift 문법 - var, let, String, Bool, numeric Type (0) | 2022.12.22 |
Swift 준비 (0) | 2022.12.22 |