0

Ok I am having trouble figuring out how to approach this.

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

What I am having trouble with is figuring out how to take the selected value from the UIPickerView and add it to the text property of my UITextField

My UIPickerView is a custom class but I added no extra functionality I only overrode it to make it possible to pass a datasource 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 it's .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 the UITextField with the 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 omitted some of the space, if some things are unclear I will gladly explain.

Every thing works perfectly with no exceptions, I just want to get the selected value out of the picker

Brian
  • 15,599
  • 4
  • 46
  • 63
Armand
  • 9,847
  • 9
  • 42
  • 75

4 Answers4

1

See the picker view delegate protocol. Implement this method:

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

In here, you can get the appropriate value from the model and update your text field.

To work out which is the current text field, you'd need to find out which of the text fields is the first responder.

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

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

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • My problem is how do I pass it back to my textfield, as I can have multiple textfields that has pickerviews in their inputView so I can't everything is dynamically built – Armand Mar 26 '12 at 14:43
  • Your edit is very simmular to what I ended up doing, I will accept your answer as it lead me in the right direction to what I ended up doing. For now I will keep it the way I did, until I have done my demo then I can optimize it some more. Thanks – Armand Mar 26 '12 at 15:21
1

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 a property

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

I then 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 textfield 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 on this question for finding the view that is the first responder

Find View that is the firstresponder

Community
  • 1
  • 1
Armand
  • 9,847
  • 9
  • 42
  • 75
0

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

Brian
  • 15,599
  • 4
  • 46
  • 63
0
- (void)pickerView:(UIPickerView *)pickerView1 didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    NSInteger selectedRow = [pickerView1 selectedRowInComponent:0];
    NSString *selectedPickerRow=[yourArrayOfElements objectAtIndex:selectedRow];





    // Handle the selection
}

Pass the String to the Text Field

WaaleedKhan
  • 685
  • 7
  • 18