带有pickerview的UItextfield作为firstresponder ios

时间:2016-11-22 08:18:01

标签: ios objective-c iphone uipickerview

我有一个UITextField,当点击时需要显示UIPickerView。此外,在pickerview中选择的数据将插入文本字段中。为了Achiceve它我现在使用subhajit的SHPickerField

3 个答案:

答案 0 :(得分:2)

我确实创建了UITextField的子类,它易于实现和使用。这是GitHub链接:

访问https://github.com/subhajitregor/SHPickerFieldExample

请参阅示例以了解如何使用。自述文件现在也已更新。

答案 1 :(得分:0)

试试这个

VideoUrl

答案 2 :(得分:0)

借助此代码,您还可以打开多个文本字段。

·H

UIPickerView *monthPickerView;

@property (strong, nonatomic)NSArray *monthArray;

@property (retain, nonatomic) IBOutlet UITextField *monthTextField;

的.m

self.monthArray=[[NSArray alloc] initWithObjects:@"one",@"two", nil];

self.monthTextField.delegate=self;

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self showPicker:textField];

 else if(textField==_monthTextField){
    if(self.monthTextField.text.length>=1){
        [monthPickerView selectRow:[self.monthArray indexOfObject:self.monthTextField.text] inComponent:0 animated:NO];
    }
}

return YES;
}


//**************show picker**************

- (IBAction)showPicker:(id)sender {
UITextField *textField=(UITextField *)sender;
monthPickerView = [[UIPickerView alloc] init];
monthPickerView.showsSelectionIndicator = YES;
monthPickerView.dataSource = self;
monthPickerView.delegate = self;
pickerToolbar = [[UIToolbar alloc] init];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
//to make the done button aligned to the right
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                               style:UIBarButtonItemStyleDone target:self
                                                              action:@selector(doneClicked:)];
[pickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

if(textField==_monthTextField){
    monthPickerView.tag=;
    self.monthTextField.inputView = monthPickerView;
    self.monthTextField.inputAccessoryView = pickerToolbar;
}

}

-(void)doneClicked:(id) sender
{
[self.view endEditing:YES];


}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

if(pickerView.tag==1){
    self.monthTextField.text = [_monthArray objectAtIndex:row];
}


}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{

if(pickerView.tag==1){
    return [_monthArray count];
}
return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{

if(pickerView.tag==1){
    return [_monthArray objectAtIndex:row];
}
return 0;
}