UItextfield with pickerview as firstresponder ios

China☆狼群 提交于 2019-12-11 08:11:49

问题


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.


回答1:


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.




回答2:


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
}



回答3:


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


来源:https://stackoverflow.com/questions/40736826/uitextfield-with-pickerview-as-firstresponder-ios

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