I'm trying to add different uipickerviews
to one view, the problem is that that the number of components is set one for all picker views:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
Is it possible to set different number of components for different uipickerviews
? The same question is for other functions, like number of rows
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int)
and source with the components themselves:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int)
how can this distinguishing of uipickerviews be implemented in swift ?
Create IBOutlet
s for your pickerViews and then check the pickerview in the function before returning the value
@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var picker2: UIPickerView!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if pickerView == picker1 {
return 2
}
else {
return 1
}
}
Edit: If you are creating your pickers programmatically, create properties for them
var picker1: UIPickerView?
var picker2: UIPickerView?
func createMyPickers() {
picker1 = UIPickerView(...
picker2 = UIPickerView(...
}
That way you can access your pickers in the delegate methods!