简体   繁体   中英

How to set PickerView selection to UITextFIeld.text using tags?

I currently have a group of text fields being created and under a certain condition instead of a keyboard the text field will display a pickerview. Once a selection is made, I need to populate the text field with the selection. I have created tags on all of the text fields, the problem is I don't know the syntax to select the text field I want.

Here is the code for creating the text field with the pickerview.

    else if([input.controlTypeName compare:@"RadioButton"] == NSOrderedSame){
        UITextField *pickerViewtext = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 280, 31)];          
        pickerViewtext.borderStyle = UITextBorderStyleRoundedRect;
        pickerViewtext.textColor = [UIColor blackColor]; //text color
        pickerViewtext.font = [UIFont systemFontOfSize:17.0];  //font size
        pickerViewtext.placeholder = placeholder;   //place holder
        pickerViewtext.backgroundColor = [UIColor whiteColor]; //background color
        pickerViewtext.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        pickerViewtext.tag = i;  //tag the textfields for future data collection
        pickerViewtext.keyboardType = UIKeyboardTypeDefault;  // type of the keyboard
        pickerViewtext.returnKeyType = UIReturnKeyDone;  // type of the return key
        pickerViewtext.clearButtonMode = UITextFieldViewModeWhileEditing;   // has a clear 'x' button to the right     
        pickerViewtext.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed

        UIPickerView *radioPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
        radioPicker.delegate = self;
        radioPicker.showsSelectionIndicator = YES;
        radioPicker.tag = i;   
        radioTag = i;

        [radioSize addObject:[NSNumber numberWithInt:5]];
        [radioList addObject:input.sourceText];     


        pickerViewtext.inputView = radioPicker;


        [inputsView addSubview:pickerViewtext];
        y = y + 50;
   }

I'm basically at a loss for what to put into my

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:    (NSInteger)component {
// Handle the selection
  }

delegate method. I thought about using the 'radioTag' variable as some kind of global. But with the possibility of multiple textfields that use a pickerview, this would only cause all of the picker views to link back to the same text field. And most tag examples I have found don't seem to apply to this or are too old and the syntax has changed. Any help would be great.

Thanks.

I have a similar type of app that uses the following and this may point you in the right direction

NSString *aVal = [activities objectAtIndex:[_emailPicker selectedRowInComponent:0]];
a=[aVal intValue];
NSString *bVal = [activities objectAtIndex:[_emailPicker selectedRowInComponent:1]];
b=[bVal intValue];
NSString *cVal = [activities objectAtIndex:[_emailPicker selectedRowInComponent:2]];
c=[cVal intValue];

It has three components, you may just use component:0, or whatever you need. The second part of each is converting to an intValue [integer value] which you may not need. Others might.

The values were placed into a mutableArray and "entered" into the picker view using:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:   (NSInteger)component
{
    if (component == 0) {

        return [activities count];
    }
    else if (component == 1) {
        return [feelings count];
    }
    else if (component==2) {
        return [addresses count];
    }
    else return 0;
}

where my three arrays were called activities, feelings and addresses and count is the number of entries in each array. I hope this gets you down the path a little further.

UITextField *textField = (UITextField *)[inputsView viewWithTag:pickerView.tag];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM