How to use one UIPickerView for multiple textfields in one view?

后端 未结 4 2082
予麋鹿
予麋鹿 2020-12-06 18:11

I have 8 textfields in one view. I want to populate each using a pickerview. Can I have 1 pickerview which will display different list of items from different arrays for dif

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 19:07

    I have tested this code with 3 textfields.

    And found perfect solution of your problem.

    1) Take one global UITextField.

    2) On delegate method of textfield edit, set global textfield to current textfield.

    3) On picker value change method, set the text of global textfield and it'll change the value of selected textfield.

    Below code is working properly.

    Enjoy coding.

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 
    {
    return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 
    {   
    return 10;
    }
    
    - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
    {
    return [NSString stringWithFormat:@"Test %d",row + 1];
    
    }
    
    - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
    {
    
    test.text = @"XYZ";
    
    }
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
    test = textField;
    
    return YES;
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {    
    [textField  resignFirstResponder];
    
    return YES;
    }
    

提交回复
热议问题