Bringing up a UIPickerview rather than keyboard input iOS
Asked Answered
J

1

15

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.

Jecon answered 30/1, 2013 at 6:22 Comment(0)
K
38

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.

Kirghiz answered 30/1, 2013 at 20:46 Comment(12)
Would I just edit this property in the textfield pressed action method?Jecon
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.Kirghiz
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.Jecon
I edited my answer to help you out. IF you need further guidance please let me know.Kirghiz
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?Jecon
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: https://mcmap.net/q/822336/-objective-c-implementing-a-uipickerview-with-a-quot-done-quot-button Look at this and try to use this solution, it will be much easier to customize and hopefully to understand.Kirghiz
@Jecon take a look at my above commentKirghiz
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.Jecon
@Jecon 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!Kirghiz
I tried already. You need at least 15 reputation. I'll come back to the post when I get there and do it then.Jecon
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?Purdah
@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).Kirghiz

© 2022 - 2024 — McMap. All rights reserved.