Popup/modal picker for a text field in iOS 7

泄露秘密 提交于 2019-12-02 15:32:59

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!