问题
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