我在互联网上查了但是找不到我的问题的答案。对不起,如果它是我找不到的地方。
所以我有一个带名字的选择器视图,每个名字都有一个值。我想在另一个计算中添加这些值。 这是代码:
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
答案 0 :(得分: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
}