6

I am attempting to create a UIPickerView programmatically and display it as the firstResponder of a textfield, however, the picker view is not showing up. textField is connected to an object in the interface builder, but pickerView is being created programatically.

class View: UIViewController {
    @IBOutlet var picker : UIPickerView = UIPickerView.alloc()
    @IBOutlet var textField : UITextField = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        picker = UIPickerView()
        picker.delegate = self
        picker.dataSource = self
        picker.backgroundColor = UIColor.blackColor()
        textField.inputView = picker
    }
}
extension View: UIPickerViewDataSource {

    func numberOfComponentsInPickerView(colorPicker: UIPickerView!) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
        return 5
    }
}

extension View: UIPickerViewDelegate {

    func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String!
    {
        return "Text"
    }
}

Why can't I see this pickerView when I run the app?

Edit: Adding a breakpoint inside the extensions does not stop the program, they are not being called.

trumpeter201
  • 8,487
  • 3
  • 18
  • 24
  • 1
    In your `viewDidLoad`, change `var picker : UIPickerView = ...` to `picker = ...`. You are creating a local (and temporary) picker. – rmaddy Jul 19 '14 at 03:17
  • I am now getting three errors on that line: Consecutive statements on a line must be separated by ';', Expected expression, and Expression resolves to an unused l-value – trumpeter201 Jul 19 '14 at 03:23
  • And if I remove that line, I get a fatal error at runtime: Can't unwrap optional.none – trumpeter201 Jul 19 '14 at 03:24
  • `UIPickerView.alloc()` is deprecated now, simply use `UIPickerView()` instead – kat Mar 17 '17 at 19:05
  • Both can be created programmatically. Here is a [well documented example in Objective C.](https://stackoverflow.com/a/46047257/3634990) – jungledev Sep 05 '17 at 05:01

3 Answers3

5

I found the problem-the code for assigning the input view doesn't include self. It should read

self.textField.inputView = picker
trumpeter201
  • 8,487
  • 3
  • 18
  • 24
2

I'm not sure why you cannot see the picker. But it's a wrong way.

To create an instance using:

 picker = UIPickerView.alloc()

In Swift:

you should use:

picker = UIPickerView()
Community
  • 1
  • 1
Louis Zhu
  • 792
  • 5
  • 12
2

I was having the same problem trying to get the picker view to show up when clicking in the textfield. My issue was that for some reason my iOS simulator had the "Connect Hardware Keyboard" checked. In the iOS menu go to Hardware -> Keyboard and make sure "Connect Hardware Keyboard" is unchecked. Feels dumb now not noticing any keyboard was popping up in the app for several hours but hopefully this will help save someone else the frustration.

Just wanted to add an edit: In the iOS simulator you can try toggling the software keyboard (command+K) This worked for me as well in this case and allowed me to keep the hardware keyboard connected. Just something to check quickly before assuming your code is incorrect.

  • Thanks, solved my problem! As yourself I spent some time figuring out why it didn't work... – Hugo Aug 04 '14 at 12:18