Setting UITextField's inputView property to a UIPicker not working in Swift

8.4k views Asked by At

Hello I am trying to pop a UIPickerView view programmatically when user clicks on textfield. I have tried this but it doesn't doing anything, it isn't working. Nothing is happening when I click the textfield

class UserProfileTableViewController: UITableViewController,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource {
 var itemPicker: UIPickerView! = UIPickerView()
    
    @IBOutlet weak var genderTxtField: UITextField!
   
    
    var gender = ["MALE","FEMALE"]
    override func viewDidLoad() {
        super.viewDidLoad()
       
        genderTxtField.delegate = self
        
        itemPicker!.delegate = self
        itemPicker!.dataSource = self
        itemPicker!.backgroundColor = UIColor.blackColor()
    
        self.genderTxtField.inputView = itemPicker
    }
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
        return 1
    }
    
    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return gender.count
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return gender[row]
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        genderTxtField.text = gender[row]
        //.hidden = true;
    }
    
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        itemPicker.hidden = false
        return false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
4

There are 4 answers

4
David Williames On BEST ANSWER

The issue in your current code is in this method:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    itemPicker.hidden = false 
    return false // <--- this is not letting the textField become editable
}

I would suggest removing this method entirely.

1
Devster101 On

Ok so I have dug out something similar I wrote, It uses a datepicker but the general code is the same.

var datePicker: UIDatePicker!
@IBOutlet var dateFrom: UITextField!

override func viewDidLoad() {
   super.viewDidLoad()
   self.setUpDatePicker()
}

func setUpDatePicker() {
        datePicker.datePickerMode = UIDatePickerMode.Date
        let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 44))
        let doneBtn = UIBarButtonItem(title: "Select", style: UIBarButtonItemStyle.Done, target: self, action: "selectDate")
        let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

        toolBar.setItems([space, doneBtn], animated: false)

        YOURTEXTFIELD.inputAccessoryView = toolBar
        YOURTEXTFIELD.inputView = datePicker             
    }

Then you add in a function to handle the done button press

func selectDate() {
        let formatter = NSDateFormatter()
        formatter.dateFormat = "dd-MM-yyyy";

        YOURTEXTFIELD.text = formatter.stringFromDate(datePicker.date)
        YOURTEXTFIELD.resignFirstResponder()
    }

Just replace YOURTEXTFIELD with your version genderTxtField and change the date picker to your picker.

Dont forget to check if everything is linked up

0
kai On
itemPicker!.backgroundColor = UIColor.blackColor() 

change to

 itemPicker!.backgroundColor = UIColor.whiteColor()

then remove

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    itemPicker.hidden = false
    return false
}

good luck!

0
Keval Vadoliya On
func textFieldShouldBeginEditing(textField: UITextField) -> Bool 
{
    itemPicker.hidden = false 
    return true
}

You just have to change false to true in func textFieldShouldBeginEditing.