0

我在互联网上查了一下,但找不到我的问题的答案。对不起,如果它是我没有找到的地方。

所以我有一个带有名称的选择器视图,每个名称都有一个值。我想在另一个计算中添加这些值。这是代码:

class ViewController: UIViewController, UIPickerViewDelegate,  UIPickerViewDataSource {

@IBOutlet var PickerPres: UIPickerView!
@IBOutlet var LabelPres: UILabel!

let PVpres:[(name: String, numb: Double)] = [("Blue",1), ("Red",2),   ("Green",3)]



func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

    return 1

}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    return PVpres[row].name
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return PVpres.count

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    LabelPres.text = PVpres[row].name

}

我也有

var color = Float (ColorTextField.text!)!
var item = Float (ItemTextField.text!)!
var source = Float (SourceTextField.text!)!

我想将分配给pickerView中项目的麻木添加到这个计算中:

Total = color * PVpres[0].numb / item* source

但它没有用

谢谢

JC

4

1 回答 1

1

要从选择器中获取所选行,您必须使用 selectedRow(inComponent component: Int) 方法。您还应该检查是否实际选择了任何行。但是此代码假定您的计算基于简单的数组索引

let selectedRow = PickerPres.selectedRow(inComponent: 0)

//check if any row is actually selected. if nothing is selected it will be -1
if selectedRow != -1 {
    Total = (color * PVpres[selectedRow].numb) / item* source
}
于 2017-05-11T19:20:49.583 回答