Try to define "numberOfComponents" for more than one Picker in one UIView?

I try it on this way, but it do not work. Can someone help me? Thank you.


func numberOfComponents( in pickerview: UIPickerView) -> Int {

pickerJahrgang.tag = 0

pickerTestjahr.tag = 1

pickerSprint.tag = 2

pickerKilmmhang.tag = 3

picker1000m.tag = 4

if pickerJahrgang.tag == 0 {return 1}

if pickerSprint.tag == 1 {return 3}

if pickerTestjahr.tag == 2 {return 1}

if pickerKilmmhang.tag == 3 {return 2}

if picker1000m.tag == 4 {return 3}

else {

return 1

}

}

Replies

Try to define "numberOfComponents" for more than one Picker in one UIView?

I presume you mean in “one UIViewController” here. That is, your view controller implements the picker data source protocol (

UIPickerViewDataSource
) and you want it to return different results for different pickers.

You could do that by switching based on the view (or, as you’ve shown, the view tag) but I’d do it by creating a small class that acts as the picker data source and then instantiate that class multiple times, once for each of my pickers.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"