请原谅新的快速。我正在创建一个BMI计算器应用程序。该应用程序允许用户使用分段控件选择英制或公制。基于分段控件,我更改了占位符文本。在“计算”按钮上,我调用扩展程序进行计算。我的问题是:在按下此按钮的操作中,如何确定选择了哪个段,以便我可以调整计算?这是我目前的代码:
@IBAction func segmentChanged(sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
textInputHeight.placeholder = "Enter height in inches"
textInputWeight.placeholder = "Enter weight in pounds"
}
else {
textInputHeight.placeholder = "Enter height in centimeters"
textInputWeight.placeholder = "Enter weight in kilograms"
}
}
@IBAction func CalulateBMIButton() {
if let height = textInputHeight.text.toDouble() {
if let weight = textInputWeight.text.toDouble() {
let bmiCalc = BMICalculator(height: height, weight: weight)
labelOutputBMI.text = "BMI is: \(round(10.0 * bmiCalc.bmi) / 10)"
labelOutputBMI.hidden = false
}
}
}
答案 0 :(得分:4)
您需要UISegmentedControl的出口
@IBOutlet weak var segmentedControl: UISegmentedControl!
然后您可以使用。
访问当前选择segmentedControl.selectedSegmentIndex