Reputation: 1816
I am trying to show a UIPicker that it is initially hidden whenever I touch/select/press my UITextField.
Here is my code so far:
class RegistrationViewController: UIViewController, UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet var getGender: UIPickerView! = UIPickerView()
@IBOutlet var genderTextField: UITextField! = nil
@IBOutlet var passTextField: UITextField!
@IBOutlet var repassTextField: UITextField!
let gender = ["Male", "Female", "Other"]
override func viewDidLoad() {
super.viewDidLoad()
genderTextField.delegate = self
passTextField.secureTextEntry = true
repassTextField.secureTextEntry = true
getGender.hidden = true
if(genderTextField.becomeFirstResponder()){
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 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 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)
{
genderTextField.text = "\(gender[row])"
}
I have everything set up so the genderTextField changes its value whenever you select the values from the getGender UIPicker. However, I did not success with trying to make the pickerView show up whenever I click the genderTextField.
Any suggestions?
Thanks a lot in advance!
Upvotes: 0
Views: 8111
Reputation: 41
Unless you are planning on using more than one UIPickerView
on the controller, you don't need to setGender.hidden = true
and then setGender.hidden = false
at all. Note that this doesn't hide collapse the picker view, it just hides view so that you are left with a blank box where the picker view or keyboard would have been.
You can add self.view.endEditing(true)
to your didSelectRow
message as follows
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
genderTextField.text = "\(gender[row])"
self.view.endEditing(true)
}
Upvotes: 1
Reputation: 193
I had been having a similar issue. It may not be the cleanest way, but I found making use of both textFieldDidBeginEditing
and textFieldDidEndEditing
can unhide and dismiss the pickerView:
func textFieldDidBeginEditing(textField: UITextField) {
if textField == genderTextField {
getGender.hidden = false
}
}
func textFieldDidEndEditing(textField: UITextField) {
if textField == genderTextField {
resignFirstResponder()
getGender.hidden = true
}
}
I am sure there is probably an easier way to achieve this, but hope it helps for now.
I've unfortunately found that the keyboard from the textField tends to hide my picker view when it's displayed at the bottom of the UI. I've had to change the input method of the UITextField to prevent this from happening (in viewDidLoad).
genderTextField.inputView = getGender
(if you experiences crashes with this, do not include getGender as a subView of your ViewController)
Hope this helps!
Upvotes: 0
Reputation: 3579
Your code is almost fine just a small fix required. You are not setting up default input method for text field.
First of all probably you don't need IBOutlet
on your picker view since you don't want it all the time to be visible in your view and UI builder but instead just pop it up a single time when text box is clicked. So it is safe to remove it.
Secondly you need to change default input to UIPickerView
and delegate it to self
getGender.delegate = self
genderTextField.inputAccessoryView = getGender
Should work, enjoy!
Upvotes: 0
Reputation: 19790
I'm not sure how you set up your getGender UIPickerView
, but it looks like you just set getGender.hidden = true
on viewDidLoad.
To make this appear again, hook up your UITextField Delegate method textFieldDidBeginEditing
and set getGender.hidden
to false
in this method.
Upvotes: 0