UIPickerView在文本字段编辑时有多个来源

时间:2010-12-01 12:48:29

标签: ios4 uipickerview uipicker

到目前为止我所拥有的是

@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];
 }
在我的.m 和

@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;

在我的.h文件中

现在文本字段不可编辑,我需要一些帮助或指导,或任何有关如何将UIPicker与多个来源连接的教程,这些来源可以在编辑文本字段时进行更改

1 个答案:

答案 0 :(得分:8)

所以我看到没有人关心:)。

我现在拥有的是3个textFields,无论何时我触摸textField1或textField2 Picker都会更改值并且没有键盘。当我触摸textField3键盘出现并且选择器被隐藏。现在,如果我通过单击return关闭键盘然后再次单击textField1选择器,但如果我不解除键盘按下单击按钮它将保持在选择器上方。我需要的是当键盘是firstResponder(我在屏幕上看到它)时隐藏它,如果我点击textField1并且只看到选择器

    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];
}