0

I need to display/show a Picker when a user taps on a textfield, I mean, it should appear a picker instead of a keyboard. Also, some sort of "done" button above the picker so the user clicks on it and the value from the picker is copied to the textfield and the picker is hidden again.

I've checked many tutorials from the web but haven't found anyone that can really help me.

I found a tutorial that pointed me in the right direction but I'm still missing to disappear the keyboard when clicking on the textfield. Dismissing UIPickerView with Done button on UIToolBar by @slev

Any ideas?

Community
  • 1
  • 1
Enyel
  • 3
  • 3
  • I just created a handy subclass to do this. https://github.com/CullenSUN/PickerTextField. Posted here if anyone is looking for it – Cullen SUN Nov 05 '12 at 05:29

1 Answers1

0

If you're not targeting iOS versions older than 3.2, it's easy. You can either assign the UIPickerView as the inputView property of the UITextField and the UIToolbar containing the 'done' button as the inputAccessoryView, or you could make a single UIView that has both the picker and the toolbar as subviews and assign that as the intputView (and leave inputAccessoryView as nil). Either way, this will animate your picker in in place of the normal keyboard when the text field is activated.

More details on this are available in the documentation.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • Thx! that worked!!! :D I used the "viewDidLoad" and "pickerDoneClicked" code from above link, and add the following line in "viewDidLoad" just as you said: myTextField.inputView = myPickerView; Now, how can I stop the user from typing in the textfield? If I mark textfield as not enable, then the code above doesn't work. – Enyel Jul 14 '11 at 22:55
  • @Enyel: Without the keyboard showing up, most users won't be able to type in it anyway (I haven't had opportunity to check if the few with an external keyboard could still type). But if necessary, you could always return NO from the delegate's textField:shouldChangeCharactersInRange:replacementString: method. – Anomie Jul 15 '11 at 10:54