Skip to content

Instantly share code, notes, and snippets.

@angelabauer
Created August 27, 2019 08:26
Show Gist options
  • Save angelabauer/43a975120dfdf2e10ad72f3bb2d112bb to your computer and use it in GitHub Desktop.
Save angelabauer/43a975120dfdf2e10ad72f3bb2d112bb to your computer and use it in GitHub Desktop.
import UIKit
class CalculateViewController: UIViewController {
@IBOutlet weak var heightLabel: UILabel!
@IBOutlet weak var weightLabel: UILabel!
@IBOutlet weak var heightSlider: UISlider!
@IBOutlet weak var weightSlider: UISlider!
//1. Create bmi property to store the calculated value.
var bmi: Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func heightSliderChanged(_ sender: UISlider) {
let height = String(format: "%.2f", sender.value)
heightLabel.text = height + "m"
}
@IBAction func weightSliderChanged(_ sender: UISlider) {
let weight = String(format: "%.0f", sender.value)
weightLabel.text = weight + "Kg"
}
@IBAction func calculatePressed(_ sender: UIButton) {
let height = heightSlider.value
let weight = weightSlider.value
//2. Calculate the bmi and store it in the bmi property.
bmi = weight / (height * height)
performSegue(withIdentifier: "goToResult", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToResult" {
let destinationVC = segue.destination as! ResultViewController
//3. Turn the calculated bmi into a String with 1 decimal place, then set it as the value of bmiValue int he destination ResultViewController.
destinationVC.bmiValue = String(format: "%.1f", bmi)
}
}
}
import UIKit
class ResultViewController: UIViewController {
var bmiValue: String?
@IBOutlet weak var bmiLabel: UILabel!
@IBOutlet weak var adviceLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//4. Use the bmiValue passed over from the CalculateViewController as the text in the bmiLabel.
bmiLabel.text = bmiValue
}
@IBAction func recalculatePressed(_ sender: UIButton) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment