Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swift中确定在发送方函数之外选择哪个分段控件_Swift_Uisegmentedcontrol - Fatal编程技术网

如何在swift中确定在发送方函数之外选择哪个分段控件

如何在swift中确定在发送方函数之外选择哪个分段控件,swift,uisegmentedcontrol,Swift,Uisegmentedcontrol,请原谅新来的斯威夫特。我正在创建一个体重指数计算器应用程序。该应用程序允许用户使用分段控件选择英制或公制。基于分段控件,我更改占位符文本。在“计算”按钮上,我调用一个分机来进行计算。我的问题是:在按下按钮的这个动作中,我如何确定选择了哪个段,以便调整计算?这是我当前的代码: @IBAction func segmentChanged(sender: UISegmentedControl) { if sender.selectedSegmentIndex == 0 { te

请原谅新来的斯威夫特。我正在创建一个体重指数计算器应用程序。该应用程序允许用户使用分段控件选择英制或公制。基于分段控件,我更改占位符文本。在“计算”按钮上,我调用一个分机来进行计算。我的问题是:在按下按钮的这个动作中,我如何确定选择了哪个段,以便调整计算?这是我当前的代码:

@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

您需要一个到UISegmentedControl的出口

@IBOutlet weak var segmentedControl: UISegmentedControl!
然后可以使用访问当前选择

segmentedControl.selectedSegmentIndex

就这样!非常感谢。我忘了我也需要把它变成一个出口,我尝试了segmentChanged.selectedSegmentIndex,但没有找到错误的地方。再次感谢,就这样!非常感谢。我忘了我也需要把它变成一个出口,我尝试了segmentChanged.selectedSegmentIndex,但没有找到错误的地方。再次感谢。