Bringing up a UIPickerview rather than keyboard input iOS

你。 提交于 2019-12-13 12:24:33

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14597902/bringing-up-a-uipickerview-rather-than-keyboard-input-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!