Using 2 uipickerviews in one viewcontroller

I have an app I developed where I’m using two pickerview box’s using the tag property: one for gender and one for a list of items. Once I choose either pickerview, it somehow increments the other pickerview to an entry with an actual value and doesn’t start from the empty entry I created at [0]. I make my first selection and when I go into the 2nd list it’s already sitting on 50-55 (first entry with a value and sits at [1]. If i dont move from [1] to another and then back to [1] the label is still blank. - If i click off of pre-selected item and go back to it, all works fine. Pasting the pickerview code below - let me know if more info is needed and thanks in advance!


Added a video as to exactly what is happening:

https://www.youtube.com/watch?v=JnHii-c1E3M&feature=youtu.be



    let gendOption = ["", "Male","Female"] //first array for first textfield
    let actOption = ["", "50%-55%", "55%-60%", "60%-65%", "65%-70%", "70%-75%", "75%-80%", "80%-85%", "85%-90%", "90%-95%"] //array for 2nd textfield
    let pickerView = UIPickerView()
    var currentTxtFldTag : Int = 10
  
   
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
   
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        //return priorityTypes.count
        if currentTxtFldTag == 10
        {
            return gendOption.count
        }
        else
        {
            return actOption.count
        }
    }
   
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        //return priorityTypes[row]
        if currentTxtFldTag == 10
        {
            return gendOption[row]
        }
        else
        {
            return actOption[row]
        }
    }
   
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //selectedPriority = priorityTypes[row]
        //activityTextBox.text = selectedPriority
        if currentTxtFldTag == 10
        {
            genderSelection.text = gendOption[row]
        }
        else
        {
            activityTextBox.text = actOption[row]
           
        }
    }
   
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
       
        if textField.tag == 10  // *** OPTION
        {
            currentTxtFldTag = 10
        }
        else  // activity OPTION
        {
            currentTxtFldTag = 20
        }
       
        pickerView.reloadAllComponents()
        return true
    }
   
    func createPickerView() {
        let pickerView = UIPickerView()
        pickerView.delegate = self
        activityTextBox.inputView = pickerView
    }

Replies

The problem (really unconvenient) with pickerViews is that didSelect is not called until you move the picker.


The way I solve this is to set the value of picker when I load them or when I show them.

There seem to be a design flaw in your code, or we miss some parts to understand.


You create a first pickerView line 3.

Then you create line 65, even though the func is never called


The simplest would be to create 2 pickerViews, one for gender, one for ***. You may use tag if you want, but not really needed

Why don't you create the pickers directly in IB ?


I did not test the following, but that should work.


    let gendOption = ["", "Male","Female"] // first array for first textfield
    let actOption = ["", "50%-55%", "55%-60%", "60%-65%", "65%-70%", "70%-75%", "75%-80%", "80%-85%", "85%-90%", "90%-95%"] //array for 2nd textfield
   var genderPickerView : UIPickerView!
   let actPickerView : UIPickerView!

// In viewDidLoad
        genderPickerView = UIPickerView()
        actPickerView = UIPickerView()
        genderPickerView.delegate = self
        genderTextBox.inputView = pickerView     // I suppose that's the name of textbox ?
        actPickerView.delegate = self
        activityTextBox.inputView = actPickerView

    // NO NEED var currentTxtFldTag : Int = 10

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1          // Works for both
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == genderPickerView
        {
            return gendOption.count
        }
        else if  pickerView == actPickerView     // safer in case you add a third picker some day
        {
            return actOption.count
        }
        else
        {
            return 0
        }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == genderPickerView
        {
            return gendOption[row]
        }
        else if  pickerView == actPickerView     // safer in case you add a third picker some day
        {
            return actOption[row]
        }
        else
        {
            return ""
        }
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView == genderPickerView

        {
            genderSelection.text = gendOption[row]
        }
        else if  pickerView == actPickerView     // safer in case you add a third picker some day
        {
            activityTextBox.text = actOption[row]
        }
        else
        {
            activityTextBox.text = actOption[row]
        }

   }

/* Following should not be needed
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

        if textField.tag == 10  // *** OPTION
        {
            currentTxtFldTag = 10
        }
        else  // activity OPTION
        {
            currentTxtFldTag = 20
        }

        pickerView.reloadAllComponents()
        return true
    }

    func createPickerView() {
        let pickerView = UIPickerView()
        pickerView.delegate = self
        activityTextBox.inputView = pickerView
    }
*/