Multiple sources for UIPickerView on textfield edi

2019-01-23 10:02发布

What i have so far is

@synthesize txtCountry,txtState;
int flgTextField;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
    [pickerView reloadAllComponents];

    // Make a new view, or do what you want here
    if(textField == txtCountry || textField == txtState){
        flgTextField = textField.tag;
        [UIView beginAnimations:nil context:NULL];
        //[pvState setFrame:CGRectMake(0.0f, 199.0f, 320.0f, 216.0f)];
        [UIView commitAnimations];  
        return NO;
    }
    else {
        return YES;
    }

}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

    if(flgTextField==1){
        return [arryCountry count];
    }
    else {
        return [arryState count];
    }

}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    if(flgTextField==1){
        return [arryCountry objectAtIndex:row];
    }
    else{
        return [arryState objectAtIndex:row];
    }

}

 - (void)viewDidLoad {
     arryCountry = [[NSMutableArray alloc] init];
     arryState = [[NSMutableArray alloc] init];
     [arryCountry addObject:@" 100 "];
     [arryCountry addObject:@" 200 "];
     [arryCountry addObject:@" 400 "];
     [arryCountry addObject:@" 600 "];
     [arryCountry addObject:@" 1000 "];
     [arryState addObject:@" a "];
     [arryState addObject:@" b "];
     [arryState addObject:@" c "];
     [arryState addObject:@" d "];
     [arryState addObject:@" e "];
      [super viewDidLoad];
 }

in my .m and

@interface Contact : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {

    IBOutlet UITextField *txtCountry;
    IBOutlet UITextField *txtState; 
    NSMutableArray *arryCountry;
    NSMutableArray *arryState;
    UIPickerView *pickerView;
}
@property(nonatomic,retain) IBOutlet UITextField *txtCountry;
@property(nonatomic,retain) IBOutlet UITextField *txtState;

in my .h file

Now the text fields are not editable and I need some help or guidance, or any tutorial on how to connect UIPicker with multiple sources that can be change when text fields are editing

1条回答
狗以群分
2楼-- · 2019-01-23 10:37

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];
}
查看更多
登录 后发表回答