trumpeter201 trumpeter201 - 1 year ago 238
iOS Question

Programmatically Create and Show UIPickerView

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() {
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.

Answer Source

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

self.textField.inputView = picker
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download