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 the titleForRow
function which was your primary problem. Also, in didSelectRow
you need to update the class property picker2Options
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.
class CarMakeModel {
func makeValues() -> [String] {
return ["Toyota", "Honda"]
}
func modelValues(make: String) -> [String] {
if make == "Toyota" {
return ["Camry", "Sienna", "Highlander"]
} else {
return ["Accord", "Civic", "Pilot"]
}
}
}
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:[String] = []
var picker2Options:[String] = []
@IBAction func SubmitButton(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
//makePicker = UIPickerView()
//modelPicker = UIPickerView()
let makeAndModel = CarMakeModel();
picker1Options = makeAndModel.makeValues()
let firstValue = picker1Options[0]
picker2Options = makeAndModel.modelValues(firstValue)
}
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 {
return "\(picker2Options[row])"
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
let makeAndModel = CarMakeModel();
let currentValue = picker1Options[row]
picker2Options = makeAndModel.modelValues(currentValue)
pickerView.reloadAllComponents()
}
}
}