简体   繁体   中英

Bringing up a UIPickerview rather than keyboard input iOS

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM