I have 3 pickers like this: the 1st one is the generalPicker, the 2nd and 3rd are the same. In the generalPicker, I have an array (menu): ["Length", "Volume", "Mass"] and every time I want to choose, for example Mass from the generalPicker, I want to get in the 2nd and 3rd pickers this array: ["Milligram", "Centigram", "Gram", "Kilogram", "Stone", "Pound", "Ounce"].
Or, if I chose in the generalPicker, let's say, Length, then I want to get in the 2nd and 3rd pickers: ["Millimeter", "Centimeter", "Meter", "Kilometer", "Foot", "Yard", "Mile"].
Can anyone give me an idea of how can I actually do that? A sample of my code (the pickers config):
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 || pickerView.tag == 1 {
return self.pickerLength.count
} else if pickerView.tag == 2 {
return self.pickerGeneral1.count
}
return 3
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 || pickerView.tag == 1{
return self.pickerLength[row]
} else if pickerView.tag == 2 {
return self.pickerGeneral1[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
self.pickerTextField.text = self.pickerLength[row]
self.view.endEditing(true)
}else if pickerView.tag == 1 {
self.pickedTextField2.text = self.pickerLength[row]
self.view.endEditing(true)
}else if pickerView.tag == 2 {
self.pickerGeneral.text = self.pickerGeneral1[row]
self.view.endEditing(true)
}
}
Arrays defs :
private var pickerMass = ["Milligram", "Centigram", "Gram", "Kilogram", "Stone", "Pound", "Ounce"]
private var pickerVolume = ["Milliliter", "Centiliter", "Liter", "Gallon", "Quart", "Pint", "Fluid ounce"]
private var pickerLength = ["Millimeter", "Centimeter", "Meter", "Kilometer", "Foot", "Yard", "Mile"]
private var pickerGeneral1 = ["Length", "Volume", "Mass"]
The following is the working code. It is certainly not the pretty way to do the work but will give an idea to resolve the issue.
I would be using only one UIPickerView instead of 3.