iosswiftdatapicker

Fill 2 different PickerView


I have 2 data pickers and I need to fill them with different data. These data pickers are instances of class AKPickerView.

I have functions to fill them, but how I can do that with different information for every data picker view?

func numberOfItemsInPickerView(_ pickerView: AKPickerView) -> Int {
    return self.titles.count
}

func pickerView(_ pickerView: AKPickerView, titleForItem item: Int) -> String {
    return self.titles[item]
}

func pickerView(_ pickerView: AKPickerView, didSelectItem item: Int) {
    print("Your favorite city is \(self.titles[item])")
}

Solution

  • I assume that you have 2 instance properties that are of type AKPickerView. In each method, you can check if the given pickerView is equals to one of them and do things according to the result.

    func numberOfItemsInPickerView(_ pickerView: AKPickerView) -> Int {
        if pickerView == self.myPicker1 {
            return data.count
        }
        return data2.count
    }