I am unable to reload the second component in my UIPickerView
that is based on the first component. I tried using the reloadAllComponents
but it either does not work or crashes. What I am trying to work on is place car makes on the left and models of their cars on the right. So when the car make changes, it would simultaneously change the right component model.
class RegistrationPage: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var passwordReTypeTextField: UITextField!
@IBOutlet weak var makePicker: UIPickerView!
@IBOutlet weak var modelPicker: UIPickerView!
@IBOutlet weak var licenseTagTextField: UITextField!
var picker1Options = []
var picker2Options = []
@IBAction func SubmitButton(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
makePicker = UIPickerView()
modelPicker = UIPickerView()
let makeAndModel = CarMakeModel();
picker1Options = makeAndModel.makeValues()
let firstValue = picker1Options.objectAtIndex(0)
picker2Options = makeAndModel.modelValues(firstValue as! String)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (component == 0) {
return picker1Options.count
} else {
return picker2Options.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if component == 0 {
return "\(picker1Options[row])"
} else {
let makeAndModel = CarMakeModel();
let currentValue = picker1Options.objectAtIndex(row)
var picker2ptions = makeAndModel.modelValues(currentValue as! String)
return "\(picker2ptions[row])"
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let makeAndModel = CarMakeModel();
let currentValue = picker1Options.objectAtIndex(row)
var picker2ptions = makeAndModel.modelValues(currentValue as! String)
pickerView.reloadAllComponents()
}
}
Thanks!
Here is a version that works. I created my own
CarMakeModel
class to test it. You were overthinking thetitleForRow
function which was your primary problem. Also, indidSelectRow
you need to update the class propertypicker2Options
and not a local variable. I changed other things. I'd recommend you compare my version to yours line by line to see the differences.Also, there is only 1 pickerView needed, and it should be connected to the one in your Storyboard. You shouldn't be instantiating your own pickerViews.