IOS7中的文本框的弹出/模式选择器 [英] Pop-up/Modal picker for a textfield in IOS7

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

问题描述

我有很多麻烦,弄清楚如何实现标准的弹出式选择器。像许多应用程序注册屏幕一样,当用户选择生日文本字段ID(如弹出式选择器)以便用户可以选择其生日时,单击完成,格式化的日期将被添加到文本字段。这似乎不应该是那么难,但似乎在ios7中似乎没有简单,清晰的标准方式来做到这一点。我搜索互联网,看到有人说使用模态,其他人说动作片,其他人说弹出窗口,还有人说一个单独的视图控制器。任何人都可以告诉我,这样做的标准方法是和/或提供一个链接到一个tut或一个关于如何实现它的代码片段?

I am having alot of trouble figuring out how to implement a standard pop-up picker. Like many apps registration screen when a user selects the birthday text field id like a pop-up picker to appear so that users can select their birthday , click done and the formatted date will be added to the text field. This doesnt seem like it should be all that hard yet, it seems there is no simple,clear, standard way of doing this in ios7 . Ive searched the internet and seen some saying to use modals, others say actionsheets, others say popups and still others say a seperate view controller.Can anyone tell me what the standard way of doing this is and/or provide a link to a tut or a snippet on how to implement it?

推荐答案

我认为标准的方式是将选择器设置为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];
}

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

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