Search code examples
iosswiftuitextfielduipickerview

Picker View from text field is failing


I am trying to display a picker view upon clicking inside my UITextField.

This is my code so far:

import UIKit

class Anywhere2ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

@IBOutlet weak var climateText: UITextField!

var climates = ["Hot","Warm","Cold"]
var picker = UIPickerView()

override func viewDidLoad() {
    super.viewDidLoad()

    picker.delegate = self
    picker.dataSource = self
    climateText.inputView = picker

}

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return climates.count
    }

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    climateText.text = climates[row]
}

But when I go to Build and Run, I continue to get this Failure alert!

2016-03-19 11:44:28.303 ABH[20523:8939349] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key pickerTextField1.'**

Any suggestions on how to fix this?


Solution

  • i tested it and it works fine. I think you problem is related to this. Try to rebind the textfield outlet or do as described in the link. Somehow your @IBOutlet weak var climateText: UITextField! is not connected. You can click on the textfield in xcode and then on the connections inspector and then check referenced outlets.

    enter image description here