2

I have a view with 8 textfields. I want each textfield to be associated with a pickerview which will have different items. This pickerview should populate each text field.

When I click on the textfield it should show the pickerview and the items associated with it and when I select the item for that textfield it should dismiss the pickerview. Then again when I click on next textfield it should show the pickerview with the items for that text field and so on...

Is it possible to do so? I am new to xcode. Any help is much appreciated. Thank you.

1 Answer 1

2

Absolutely possible. Here's my best recommendation:

  1. Create 8 arrays--one for each picker view. Within these arrays, contain the options that the user should be able to choose from. This can be easily accomplished with NSArray.
  2. Set up your textfields in Interface Builder (i.e. in your Storyboard file), and connect an IBAction from each textfield to a function within the view controller's code.
  3. Within each of these functions, instantiate a new UIPickerView with the contents of the array corresponding to the current picker.
  4. After the user has selected the item, set the textfield's content to the array value contained within the index selected by the picker.
  5. Remove the picker from the current view.

As I here you're new to Xcode, let me know if you need any additional explanation.

BTW, here is the class reference for UIPickerView: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPickerView_Class/Reference/UIPickerView.html

UPDATE: In the function that is triggered when the textfield is tapped, allocate and instantiate the picker. Then, add it to your view. Afterwards, simply remove it from the view when the user has finalized his selection.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot! I will do this and get back incase I have any problems. Also can you tell me how to hide the pickerview initially and display it only when the textfield is clicked?
Thank you, can you please post a sample code for one function of IBAction? I am finding it difficult to do it?
I see you have accepted my answer. Would you still like sample code?
No, thank you. I could do it using your solution partly. Thanks!
Sorry to comment on a older thread but @TheKraken would you mind posting that sample code for my benefit? Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.