IOS7中的textfield弹出/模态选择器

时间:2022-01-12 11:30:17

I am having a lot 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 doesn't seem like it should be all that hard yet, it seems there is no simple, clear, standard way of doing this in ios7. 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 and/or provide a link to a tut or a snippet on how to implement it?

我在如何实现一个标准的弹出选择器上遇到了很多麻烦。就像许多应用程序注册界面一样,当用户选择生日文本字段id时,就像弹出式选择器一样,用户可以选择自己的生日,点击done,格式化的日期就会被添加到文本字段中。这似乎还不应该那么困难,似乎在ios7中还没有简单、清晰、标准的方法来实现这一点。我在网上搜索过,有人说要用modals,有人说是actionsheets,有人说是弹出窗口,还有人说是单独的视图控制器。谁能告诉我这是什么标准的方法,或者提供一个tut的链接或者是如何实现它的代码片段?

2 个解决方案

#1


69  

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];
}

#2


15  

The ActionSheetPicker-3.0 library seems to do this pretty well.

ActionSheetPicker-3.0库似乎做得很好。

IOS7中的textfield弹出/模态选择器

IOS7中的textfield弹出/模态选择器

#1


69  

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];
}

#2


15  

The ActionSheetPicker-3.0 library seems to do this pretty well.

ActionSheetPicker-3.0库似乎做得很好。

IOS7中的textfield弹出/模态选择器

IOS7中的textfield弹出/模态选择器