How to use two UIPickerViews in one view controller?

dom999999 picture dom999999 · Dec 24, 2014 · Viewed 37.3k times · Source

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.

Answer

LAOMUSIC ARTS picture LAOMUSIC ARTS · Feb 19, 2015

Here is my solution:

  • in the storyboard, add two UIPickerView instances to your view
  • set the first picker's tag as 1 and set 2 for the second picker under the "Attributes Inspector"
  • control + drag from each picker to the top yellow view controller icon and choose 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])"
        }
    }