Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use two UIPickerViews in one view controller?

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.

like image 933
dom999999 Avatar asked Dec 24 '14 21:12

dom999999


People also ask

How do I create a new view controller?

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.


1 Answers

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])"     } } 
like image 171
5 revs, 4 users 69% Avatar answered Oct 22 '22 13:10

5 revs, 4 users 69%