Multiple sources for UIPickerView on textfield editing

混江龙づ霸主 提交于 2019-11-29 00:41:10

So i see no one cares :)

what i have now is 3 textFields and whenever i touch textField1 or textField2 Picker changes values and there is no keyboard. When i touch textField3 keyboard appears and the picker goes hidden.Now if i dismiss the keyboard by clicking return and then click textField1 picker appears again, but if i dont dismiss the keyboard BY CLICKING BUTTON it stays over the picker. What I need is when the keyboard is firstResponder (and i see it on the screen) to hide it if i click on the textField1 and only to see the picker

    int variabla;

        -(void)textFieldDidBeginEditing:(UITextField *)textField{
            [pickerView setHidden:YES];
            if (textField1.editing == YES) {
                [textField1 resignFirstResponder];
                [pickerView setHidden:NO];
                variabla = 1;
            }else if (textField2.editing == YES) {  
                [textField2 resignFirstResponder];
                [pickerView setHidden:NO];
                variabla = 2;
            }   
            NSLog(@"variabla %d",variabla); 
            [pickerView reloadAllComponents];
        }

        - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
        {
            return 1;
        }
        - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
        {
            if (variabla == 1) {
                return [pickerArray1 count];
            }else if (variabla == 2) {
                return [pickerArray2 count];
            }else {
                return 0;
            }   
        }
        - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
        {   
            if (variabla == 1) {
                return [pickerArray1 objectAtIndex:row];        
            }else if (variabla == 2) {
                return [pickerArray2 objectAtIndex:row];
            }else {
                return 0;
            }   
        }
        - (void)textFieldShouldReturn:(UITextField *)textField{ 
            [textField resignFirstResponder];
        }
- (void)viewDidLoad {
    [super viewDidLoad];
    [pickerView setHidden:YES];
    pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"0", @"1", @"2", nil];
    pickerArray2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", @"5", nil];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!