如何确定在 swift 中的发送者功能之外选择了哪个分段控件
how to determine which segmented control is selected outside of sender func in swift
请原谅 swift-ness 的新手。我正在创建一个 BMI 计算器应用程序。该应用程序允许用户使用分段控件 select 英制或公制。基于分段控件,我更改了占位符文本。在 'Calculate' 按钮上,我调用了一个分机来进行计算。我的问题是:在按下按钮的这个动作中,如何确定select编辑了哪个段,以便调整计算?这是我当前的代码:
@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
}
}
}
您需要 UISegmentedControl 的出口
@IBOutlet weak var segmentedControl: UISegmentedControl!
然后您可以使用访问当前选择。
segmentedControl.selectedSegmentIndex
请原谅 swift-ness 的新手。我正在创建一个 BMI 计算器应用程序。该应用程序允许用户使用分段控件 select 英制或公制。基于分段控件,我更改了占位符文本。在 'Calculate' 按钮上,我调用了一个分机来进行计算。我的问题是:在按下按钮的这个动作中,如何确定select编辑了哪个段,以便调整计算?这是我当前的代码:
@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
}
}
}
您需要 UISegmentedControl 的出口
@IBOutlet weak var segmentedControl: UISegmentedControl!
然后您可以使用访问当前选择。
segmentedControl.selectedSegmentIndex