Search code examples
iosswiftuipickerview

How to set up UIPickerView values using info procured from viewDidLoad?


I want to set the initial values for a UIPickerView based on the picker functions:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {//return number}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {//return String}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {//return number}

I have left the function in generic form, because their exact code isn't important to the question. The general question/problem that I have is that the values in the functions are supposed to be based on information pulled from a DB. I have a function set up to be called in the viewDidLoad() that pulls the information. My question is that, basically, will the function in the viewDidLoad() be called (and thus have grabbed the information) before the the pickerView functions are called? I need to know if the viewDidLoad() query can pull the information to be used by the pickerView functions, before the pickerView functions run. If it happens afterward, the functions will not have the data they need. How do I set this up?


Solution

  • When the database query is complete, simply reload the picker:

    pickerView.reloadAllComponents()
    

    From the documentation:

    Calling this method causes the picker view to query the delegate for new data for all components.