iOS 7中文本字段的弹出/模态选择器 [英] Popup/modal picker for a text field in iOS 7

查看:97
本文介绍了iOS 7中文本字段的弹出/模态选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搞清楚如何实现标准弹出选择器方面遇到了很多麻烦。像许多应用程序的注册屏幕一样,当用户选择生日文本字段时,我想要一个弹出选择器出现以便用户可以选择他们的生日,点击完成并将格式化的日期添加到文本字段。这看起来似乎不应该那么难,但似乎在iOS 7中没有简单,明确,标准的方法。

I am having a lot of trouble figuring out how to implement a standard popup picker. Like many apps' registration screen when a user selects the birthday text field I'd like a popup picker to appear so that users can select their birthday, click done and the formatted date will be added to the text field. This doesn't seem like it should be all that hard, yet it seems there is no simple, clear, standard way of doing this in iOS 7.

我是搜索互联网并看到一些说使用模态,其他人说操作表,其他人说弹出窗口,还有其他人说单独的视图控制器。

I've searched the internet and seen some saying to use modals, others say actionsheets, others say popups and still others say a separate view controller.

任何人都可以告诉我标准的方式是什么这样做是关于如何实现它的一个片段?

Can anyone tell me what the standard way of doing this is or a snippet on how to implement it?

推荐答案

我认为标准的方式,就是设置picker作为文本字段的inputView。

I think the "standard" way, is to set the picker as the inputView of the text field.

UIPickerView *picker = [[UIPickerView alloc] init];
self.textField.inputView = picker;

它会弹出底部,就像键盘在文本字段中触摸时一样。

It will pop up front the bottom, just like the keyboard does when you touch in the text field.

这是一个如何使用选择器作为输入视图的简单实现:

Here's a simple implementation of how to use a picker as an input view:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (strong,nonatomic) NSArray *theData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIPickerView *picker = [[UIPickerView alloc] init];
    picker.dataSource = self;
    picker.delegate = self;
    self.tf.inputView = picker;
    self.theData = @[@"one",@"two",@"three",@"four"];
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.theData.count;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return  1;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.theData[row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.tf.text = self.theData[row];
    [self.tf resignFirstResponder];
}

这篇关于iOS 7中文本字段的弹出/模态选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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