调出UIPickerview而不是键盘输入iOS [英] Bringing up a UIPickerview rather than keyboard input iOS

查看:48
本文介绍了调出UIPickerview而不是键盘输入iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我希望用户单击文本字段,它会弹出一个填充的pickerview而不是键盘.这也将需要一个带有完成按钮的工具栏.我目前将字段设置为输出和操作,仅此而已.我也有一个代码中的动作表,用于用户提交内容时的操作表,如果这与可能为此使用动作表有任何区别的话.

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.

我尝试研究此主题,但是99%的主题都是使用日期选择器而不是pickerviews(或非常旧的代码).

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现在具有inputView属性.在这里,您可以为其分配想要的显示,包括UIPickerView.不过,您必须设置pickerview,并且必须在.h中实现UITextFieldDelegateUIPickerViewDataSource:

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;

由于实现了UIPickerView接口,因此现在必须实现以下方法:

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.

这篇关于调出UIPickerview而不是键盘输入iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆