Bringing up a UIPickerview rather than keyboard input iOS

12,662

UITextField now has an inputView property. Here you can assign it a display that you want including a UIPickerView. You must setup the pickerview though and must implement UITextFieldDelegate and UIPickerViewDataSource in your .h:

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

Then create the picker view and assign it to the textfield.

UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.dataSource = self;
pickerView.delegate = self;
// ... ...
self.pickerTextField.inputView = pickerView;

Because you implemented the UIPickerView interfaces you must now implement these methods:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

You should then be good to go. Check out the documentation for some other methods if you need more information.

Share:
12,662
JohnV
Author by

JohnV

Updated on June 08, 2022

Comments

  • JohnV
    JohnV about 2 years

    Basically I would like to have the user click in the text field and it bring up a populated pickerview rather than a keyboard. This would also need a toolbar with a done button as well Im presuming. I currently have the field set as an output and action and not much more. I also have an actionsheet in code being used for when the user submits something as well if that makes any difference to possibly using an actionsheet for this as well.

    I tried researching this topic but 99% of the topics were with datepickers rather than pickerviews (or very old code).

    Here is an image of what it looks like for reference.

  • JohnV
    JohnV over 11 years
    Would I just edit this property in the textfield pressed action method?
  • Firo
    Firo over 11 years
    I would probably do it in the viewDidLoad for the ViewController. There is really no point in setting it every time the button is pressed unless you are going to switch between a UIPicker and a normal keyboard.
  • JohnV
    JohnV over 11 years
    It seems to work. It brings up a black box rather than the keyboard. I don't know how I would go about populating this or adding a toolbar to check for done. Any ideas? I have the NSArray made but I don't know how to link it to it since it's not really a made thing.
  • Firo
    Firo over 11 years
    I edited my answer to help you out. IF you need further guidance please let me know.
  • JohnV
    JohnV over 11 years
    Ah! I was forgetting pickerView.delegate = self; Now my problem is adding the toolbar and button. This is what I was starting with to just add the bar. 1. UIToolbar *doneBar = [[UIToolbar alloc] init]; 2. doneBar.barStyle = UIBarStyleBlackTranslucent; 3. [pickerView addSubview:doneBar]; Any reason why it wouldn't show up?
  • Firo
    Firo over 11 years
    The reason it is not showing up is because you did not init it with a frame size so it is probably setting the frame of the toolbar to 0,0 with a height and width of 0. Even if you setup the frame though there is still no button on that toolbar. So you would need to instantiate that and also programmatically setup an action for it to dismiss the textfield's firstresponder. I was going to give you another solution but found it here: stackoverflow.com/a/10705161/1429262 Look at this and try to use this solution, it will be much easier to customize and hopefully to understand.
  • Firo
    Firo over 11 years
    @JohnV take a look at my above comment
  • JohnV
    JohnV over 11 years
    Sorry I had forgotten to respond. Thank you again. The link was very! helpful and I figured it all out eventually. Thanks for your time.
  • Firo
    Firo over 11 years
    @JohnV If it helped out could you accept my answer by pressing the check up by the arrows next to my answer? Also glad it worked out!
  • JohnV
    JohnV over 11 years
    I tried already. You need at least 15 reputation. I'll come back to the post when I get there and do it then.
  • Erika Electra
    Erika Electra over 9 years
    Solution works perfectly for a single UIPickerView, but could you please elaborate how to handle multiple UIPickerViews? Seems like I can implement each delegate & data source method only once per ViewController, but what if I have several UIPickerViews in the same ViewController?
  • Firo
    Firo over 9 years
    @Cindeselia, Each delegate/datasource method passes you the sender. So you need to check for this in the method and return appropriate results (or perform specific actions). if (sender == self.pickerOne) {} else if (sender == self.pickerTwo) {}. That make sense? Here is a more sophisticated solution. A bit confusing at first, but when it is implemented correctly it is actually really clean and nice (this goes over UITableViews but the idea is the same).