The best way was:
Here is my solution: – on the Storyboard, add 2 PickerView to your View – set the 1st picker´s tag as #1 & #2 for the 2nd picker under the Attributes Inspector – CTRL + drag from each picker to the top yellow View Controller icon and choose dataSource.Repeate the same choosing delegate – repeate the above for the other picker too – add pickerview & pickerviewdelegation to your ViewController class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate { in your ViewController class, create empty arrays for the pickers: var picker1Options = [] var picker2Options = [] on viewDidLoad() method, populate the arrays with your content: picker1Options = ["Option 1","Option 2","Option 3","Option 4","Option 5"] picker2Options = ["Item 1","Item 2","Item 3","Item 4","Item 5"] implement the delegate & pickerview methods: func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 1 } func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if (pickerView.tag == 1){ return picker1Options.count }else{ return picker2Options.count } } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { if (pickerView.tag == 1){ return "\(picker1Options[row])" }else{ return "\(picker2Options[row])" } } |
from https://stackoverflow.com/questions/27642164/how-to-use-2-uipickerviews-in-one-view-controller