I have two UIPickerControllers in one view controller. I can get one to work, but when I add a second, my app crashes. Here is the code I use for one picker view:
import UIKit class RegisterJobPosition: UIViewController, UIPickerViewDelegate { @IBOutlet weak var positionLabel: UILabel! var position = ["Lifeguard", "Instructor", "Supervisor"] override func viewDidLoad() { super.viewDidLoad() } func numberOfComponentsInPickerView(PickerView: UIPickerView!) -> Int { return 1 } func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int { return position.count } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return position[row] } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { positionLabel.text = position[row] } } Now, how can I get a second picker to work? Say my second picker view is called location (the other one is called position). I tried duplicating the code within the picker view methods for location but it doesn't work.
To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.
Here is my solution:
UIPickerView instances to your view1 and set 2 for the second picker under the "Attributes Inspector"dataSource. Repeat the same choosing delegate add UIPickerViewDataSource and UIPickerViewDelegate to your view controller:
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { in your view controller class, create empty arrays for the pickers:
var picker1Options = [] var picker2Options = [] In viewDidLoad(), 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 and data source 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])" } } If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With