I am trying to create a text box that when it is selected a UIPickerView opens up with choices to select from. Once selected, the UIPickerView hides and the selected item is
How about in your didSelectRow method you resignFirstResponder?
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = bizCat[row]
pickerBizCat.resignFirstResponder()
}
// pressing the button again would hide the uipickerview. when pressed the first time, update the button's label to "done" , "hide" or whatever suits u!
@IBAction func propertyTypeButtonPressed(sender: UIButton)/* the name of your button's action*/
{
count++; //declare it first
ViewContainigPickerView.hidden = false
self.view.endEditing(true)
if (count == 2)
{
ViewContainingPickerView.hidden = true /* if you placed your picker on a separate view for simplicity*/
count = 0;
}
}
Swift 4 version
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
textField.delegate = self
textField.inputView = pickerView
}
And the extensions
// MARK: - UIPickerViewDelegate
extension ViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
pickerView.isHidden = false
return false
}
}
// MARK: - UIPickerViewDelegate
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myItems.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return myItems[row].name
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textField.text = myItems[row].name
pickerView.isHidden = true
}
}
If I understood well your question, you want:
UITextField
which display a text selectedUITextField
UITextField
This is the complete code to manage it, you just have to link the delegate of your UITextField
:
@IBOutlet var textfieldBizCat: UITextField!
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()
var bizCat = ["Cat One", "Cat Two", "Cat Three"]
override func viewDidLoad() {
super.viewDidLoad()
pickerBizCat.hidden = true;
textfieldBizCat.text = bizCat[0]
}
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return bizCat.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return bizCat[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = bizCat[row]
pickerBizCat.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickerBizCat.hidden = false
return false
}
What I changed from your code:
UITextFieldDelegate
to display the picker when the UITextField
is selectedUITextField
UITextField
when any item is selected