4

How can i make a UIPickerView that appears when i select a TextField, and the input of this comes into TextField if i select a value of UIPickerView?

How to do this? Have you an example of this?

lukso
  • 589
  • 5
  • 13
  • 31

2 Answers2

2

Create the UIPickerView and set it as the inputView of your text field. It will then be animated on screen for you when you start editing the text field. The picker view delegate methods can be used to update the text field's value.

You will also want to create a toolbar with a done button and set this as the inputAccessoryView - this will allow you to dismiss the picker when you have selected your value. Again, this will appear automatically above the picker view when the text field begins editing.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • yes but this method let the user to edit the text, while the picker should be the only source of data. i don't want that the user add text – Gaucho May 24 '15 at 14:51
  • 1
    No it doesn't. How do they edit the text without a keyboard? You can disable any clipboard actions. – jrturton May 24 '15 at 16:42
  • you're right, but a long press may cause the copy paste popup to appear. i want to avoid also the copy-paste functions.. – Gaucho May 24 '15 at 17:02
  • http://stackoverflow.com/questions/1426731/how-disable-copy-cut-select-select-all-in-uitextview – jrturton May 24 '15 at 17:05
  • i read that question. there is no working solution for my case in that thread. i implemented canPerformAction but it applies to all the textViews and textFields... – Gaucho May 24 '15 at 17:14
  • Then you're doing it wrong, probably in a category instead of a subclass. – jrturton May 24 '15 at 17:33
1

Please take a look at this example http://www.techotopia.com/index.php/An_iOS_4_iPhone_UIPickerView_Example

You can initially hide the picker by picker.hidden=YES; in your viewdidload method and for showing it when touch on the textfield you can write picker.hidden=NO; inside uitextfielddidstartediting method. Thanks.

Melbourne
  • 531
  • 3
  • 24