iOS 7 中文本字段的弹出/模式选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20438237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup/modal picker for a text field in iOS 7
提问by ChuckKelly
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.
我在弄清楚如何实现标准弹出选择器时遇到了很多麻烦。与许多应用程序的注册屏幕一样,当用户选择生日文本字段时,我希望出现一个弹出选择器,以便用户可以选择他们的生日,点击完成,格式化的日期将添加到文本字段中。这似乎不应该那么难,但在 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?
谁能告诉我这样做的标准方法是什么或如何实现它的片段?
回答by rdelmar
I think the "standard" way, is to set the picker as the inputView of the text field.
我认为“标准”方式是将选择器设置为文本字段的 inputView。
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];
}