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!
Any suggestions on how to fix this?
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.