Created
August 27, 2019 08:26
-
-
Save angelabauer/43a975120dfdf2e10ad72f3bb2d112bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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