Search code examples
swiftuipickerview

UIPickerView not loading


This UIPicker's data isn't loading, I get the correct number of rows, but a "?" for each element instead of the values in the array pickerValues. I'm new to Swift, hoping I just missed something obvious. Xcode 9/Swift4.

import UIKit


class ViewController3: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 4
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
         return 1
    }


    @IBOutlet weak var picker: UIPickerView!

    let pickerValues = ["Choice 1", "Choice 2", "Choice 3","Choice 4"]


    override func viewDidLoad() {
        super.viewDidLoad()

        picker.dataSource = self

        func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }     
   }       

}

Edit: I disagree that my question is a dupe. It deals with the same topic, but in my case the picker delegate method was missing. Not so with the linked question.


Solution

  • You need to implement

    func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String?  {
    
        return pickerValues[row]
    
    }