UIPickerView and UITextField get value from UIPickerView

Well, it's hard for me to figure out how to do this.

I have a UITextField that I add to the UITableView dynamically according to the definitions of the XML file, in the XML file I can specify the list as one of the data types, and what I do is add a UIPickerView that displays the list in the inputView of my particular UITextField.

I'm having problems with how to output the selected value from the UIPickerView and add it to the text property of my UITextField

My UIPickerView is a custom class, but I have not added any additional features. I just redefined it to allow the data source to be passed to the UIPickerView as a property.

Here is my PickerViewDataSourceAndDelegate.h

#import <Foundation/Foundation.h> @interface PickerViewDataSourceAndDelegate : UIPickerView <UIPickerViewDelegate, UIPickerViewDataSource> @property (nonatomic, strong) NSMutableArray *pickerData; @end 

And the .m file

 #import "PickerViewDataSourceAndDelegate.h" @implementation PickerViewDataSourceAndDelegate @synthesize pickerData; -(id)init { if (self = [super init]) { self.pickerData = [[NSMutableArray alloc] init]; } return self; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { return [self.pickerData count]; } - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [self.pickerData objectAtIndex:row]; } @end 

This is how I add a UITextField using a UIPickerView as inputView

  for(NodeProperty *nodeproperty in node.properties) { if(nodeproperty.flowDirection == (FlowDirection*)Output) { if([nodeproperty.typeName isEqualToString:@"System.String"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] == 0) { ... } else if([nodeproperty.typeName isEqualToString:@"System.String"] && nodeproperty.extendedType == (ExtendedType*)MultilineText && [nodeproperty.enumValues count] == 0) { ... } else if([nodeproperty.typeName isEqualToString:@"System.Boolean"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] == 0) { ... } else if([nodeproperty.typeName isEqualToString:@"System.Double"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] == 0) { ... } else if([nodeproperty.typeName isEqualToString:@"System.String"] && nodeproperty.extendedType == (ExtendedType*)None && [nodeproperty.enumValues count] > 0) { UILabel *fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 290, 20)]; fieldLabel.text = nodeproperty.name; [rowsInSectionLabels addObject:fieldLabel]; PickerViewDataSourceAndDelegate *pickerDataDel = [[PickerViewDataSourceAndDelegate alloc] init]; pickerDataDel.pickerData = nodeproperty.enumValues; pickerDataDel.dataSource = pickerDataDel; pickerDataDel.delegate = pickerDataDel; UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 25, 290, 30)]; [textField setBorderStyle:UITextBorderStyleRoundedRect]; textField.inputView = pickerDataDel; [rowsInSection addObject:textField]; } else { .... 

For space, I missed some space, if some things are unclear, I will gladly explain.

Everything works fine without any exceptions, I just want to get the selected value from the collector

0
source share
4 answers

See the delegate view protocol for the collector view . Implement this method:

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 

Here you can get the corresponding value from the model and update the text box.

To work out what is the current text field, you need to figure out which of the text fields is the first responder.

Have all the text fields in an IBOutletCollection or array, iterate through them and find the one that is the first responder.

Or, if you have text field delegates, you can set ivar for the current text field in the didBeginEditing delegation method and use this in the viewer delegate method above.

+1
source

I found my own solution that words like a charm, I had to create a delegate method to pass the value back to my UITextField.

First I declared a protocol in my PickerViewDataSourceAndDelegate.h

 @protocol PickerRowSelected -(void) selectedARowAndValueIs:(NSString*)aValue; @end 

Then I added a member:

 id <PickerRowSelected> adelegate; 

and added property

 @property (nonatomic, strong) id<PickerRowSelected> adelegate; 

Then I added this function

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { [adelegate selectedARowAndValueIs:[self.pickerData objectAtIndex:row]]; } 

And in the controller that contains the text box, I added:

 pickerDataDel.adelegate = self; 

And implemented my new delegate method

 -(void) selectedARowAndValueIs:(NSString *)aValue { UITextField *aview = (UITextField*)[self.view findViewThatIsFirstResponder]; aview.text = aValue; } 

See my answer to this question to find the first responder.

Find the first responder

+1
source

Use -[UIPickerView selectedRowInComponent:] to determine which row is selected, and then return the object to this index in your data array.

0
source
 - (void)pickerView:(UIPickerView *)pickerView1 didSelectRow: (NSInteger)row inComponent:(NSInteger)component { NSInteger selectedRow = [pickerView1 selectedRowInComponent:0]; NSString *selectedPickerRow=[yourArrayOfElements objectAtIndex:selectedRow]; // Handle the selection } 

Pass a string to a text field

0
source

All Articles