Using Two UI Pickers in 1 VC, Handling Delegate methods

90 views Asked by At

I have 2 pickers in 1 VC and know that this should be handled by using if else to identify which delegate methods each should follow. However Im new to getting my head around this and im not sure what i should be returning on my methods in order to have them work correctly.

Here is the code, I have just used 0 and "" to prevent code errors for now until I can get the correct returns sorted out.

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    if pickerView == repsPicker {
        return 1
    } else if pickerView == setsPicker {
        return 1
    }
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == repsPicker {
        return repPickerValues.count
    } else if pickerView == setsPicker {
        return self.setsPickerValues.count
    }
    return 0
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == repsPicker {
        return repPickerValues[row]
    } else if pickerView == setsPicker {
        return self.setsPickerValues[row]
    }
    return ""
}
3

There are 3 answers

1
Matt On BEST ANSWER
let repPickerValues = ["America", "Mexico", "Canada"]
let setsPickerValues = ["India", "China", "Malaysia", "Singapore"]

If these are your values, then you will have 3 options on 1st picker and 4 options on 2nd picker.

Adding below lines will call those functions

repsPicker.delegate = self
setsPicker.delegate = self

And your view controller class should conform to UIPickerViewDelegate and UIPickerViewDataSource. Like

class ViewController: UIPickerViewDelegate, UIPickerViewDatSource {

}

This function will tell picker view how many options are to be shown

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == repsPicker {
        return repPickerValues.count
    } else if pickerView == setsPicker {
        return self.setsPickerValues.count
    }
    return 0
}

This function is responsible for placing values in picker view

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == repsPicker {
        return repPickerValues[row]
    } else if pickerView == setsPicker {
        return self.setsPickerValues[row]
    }
    return ""
}

Is this what you are asking for?

1
Amani Elsaed On

You can set tag to each picker so that you can differentiate between them. Set pickerView.tag with two different values, and in each delegate method check the tag value to determine the return value.

0
Bogdan Farca On

I can see several options here :

  1. Use different delegate objects for each picker

    picker1.delegate = firstDelegate()
    picker2.delegate = secondDelegate()
    

    Implement the desired delegate methods differently in each of the delegate objects.

  2. Keep a reference to each picker in your view controller (or IBOutlet)

    let picker1 = ...
    let picker2 = ...
    

    Then in your delegate method(s)

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == picker1 { 
            ... 
        }
    }