UITextField点击之后弹出UIPickerView

最近在做一个项目, TextField输入时可以弹出pickerview, 如果YES/NO, MALE/FEMALE的类似选项, 所以自己研究做了一个PickerViewTextField;

  实现原理: 主要是新建UIPickerView赋给textField.inputView即可, 是不是很简单哈哈。 代码如下:

1. .h文件:

#import 

@interface PickViewTextField : UITextField
{
    UIPickerView *pickView;
}

@property (nonatomic, strong) NSArray *dataArray;// pickerView的数据源, 比如 yes, no

- (void)setSelectRow:(NSInteger)index;// 选中列

@end


2. .m文件:

#import "PickViewTextField.h"

@implementation PickViewTextField{
    UIToolbar *inputAccessoryView;// 键盘上方的toolbal, 用于加入done按钮完成输入
}

@synthesize dataArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)setSelectRow:(NSInteger)index
{
    if (index >=0 ) {
        [pickView selectRow:index inComponent:0 animated:YES];// 选中哪一列
    }
}

- (void)drawRect:(CGRect)rect
{
    pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 120)];// 新建pickerView, 我是在3.5上运行的, 6/6plus或许宽度不同.
    pickView.dataSource = self;
    pickView.delegate = self;
    self.inputView = pickView;// 重点! 这样点击TextField就会弹出pickerView了.
    
    / default selected item /
    [self setText:[dataArray objectAtIndex:0]];// 设置TextField默认显示pickerView第一列的内容
}

#pragma mark - UIPickerView dataSource, delegate
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [dataArray count];
}

-(NSString ) pickerView:(UIPickerView )pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [dataArray objectAtIndex:row];
}

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

#pragma mark - inputAccessoryView with toolbar
- (BOOL)canBecomeFirstResponder {
 return YES;
}

- (void)done:(id)sender {
    [self resignFirstResponder];
 [super resignFirstResponder];
}
/* 创建toolbar */
- (UIView *)inputAccessoryView {
 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return nil;
 } else {
  if (!inputAccessoryView) {
   inputAccessoryView = [[UIToolbar alloc] init];
   inputAccessoryView.barStyle = UIBarStyleBlackTranslucent;
   inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
   [inputAccessoryView sizeToFit];
   CGRect frame = inputAccessoryView.frame;
   frame.size.height = 30.0f;
   inputAccessoryView.frame = frame;
            
   UIBarButtonItem *doneBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
   UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            
   NSArray *array = [NSArray arrayWithObjects:flexibleSpaceLeft, doneBtn, nil];
   [inputAccessoryView setItems:array];
  }
  return inputAccessoryView;
 }
}


这样就可以调用该PickerViewTextField即可:弹出效果如下:

UITextField点击之后弹出UIPickerView_第1张图片

这里没看到textField, 被键盘挡住了 - -,  后期附上解决方案. 点击done, 键盘消失.


接受大家批准指正。

你可能感兴趣的:(IOS)