UItextfield with pickerview as firstresponder ios

2019-07-29 22:58发布

I had a UITextField which when tapped need to show a UIPickerView. Also the data selected in pickerview will be inserted in the textfield. To Achiceve it I am now using subhajit's SHPickerField.

3条回答
相关推荐>>
2楼-- · 2019-07-29 23:38

I do have created a subclass of UITextField, it is easy to implement and use. Here is the GitHub link:

Visit https://github.com/subhajitregor/SHPickerFieldExample

Please see the example to see how to use. The Readme file is also updated now.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-29 23:59

Try this

 let myPicker = UIPickerView()
 @IBOutlet var myText: UITextField!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    myText.inputView = myPicker

}


 @IBAction func textPiker(_ sender: UITextField) {

    sender.inputView = myPicker
}
查看更多
劳资没心,怎么记你
4楼-- · 2019-07-30 00:01

With the help of this code you can also open multiples text fields.

.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;
}
查看更多
登录 后发表回答