-1

私は 2 つを作成しましたが、値のUITextFields異なる 2 つを作成したいのですが、個人をそれぞれに具体的にマッピングする方法を教えてください。何か助けはありますか?UIPickerViewstextFieldstextFieldspickerView

4

3 に答える 3

1

textField.inputviewこのようにそれぞれのオプションを選択するのはどうでしょうか。

textField.inputView = pickerView1;
textField1.inputView = pickerView2;

この状況でコードを書くと、2 つ作成する代わりにpickerView、1 つ作成してfor eachpickerviewを変更します。datasourcetextfield

于 2013-02-19T14:57:45.823 に答える
0

最初に、差分ピッカーに表示する異なる値を含む 2 つの配列を作成します。たとえば、 aray1 と ary2 とします。

これを.hファイルに追加します

  @interface YourViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate, UIPickerViewDataSource>
    {

        UIToolbar* keyboardDoneButtonView;
        UIPickerView *pickerView;
        NSString * txtFieldSelected;
        NSArray *ary2;
        NSArray *ary1;
    }

そして.hファイルで

 - (void)viewDidLoad

        {
            [super viewDidLoad];
            firstTF.delegate = self;
            secondTF.delegate = self;
            ary1 =[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil];
           ary2 =[NSArray arrayWithObjects:@"a",@"s",@"d",@"f",@"g",@"h",@"x",@"c", nil];
        }

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

        if (textField == firstTF) {
            txtFieldSelected = @"firstTF";
        }else if (textField == secondFF) {
            txtFieldSelected = @"secondTF";
        }
        CGRect pickerFrame = CGRectMake(0, 40, 0, 0);  

        pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];  
        pickerView.showsSelectionIndicator = YES;  
        pickerView.dataSource = self;  
        pickerView.delegate = self;  

        keyboardDoneButtonView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];  
        keyboardDoneButtonView.barStyle     = UIBarStyleBlackTranslucent;  
        [keyboardDoneButtonView sizeToFit];  

        NSMutableArray *barItems = [[NSMutableArray alloc] init];

        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];  
        [barItems addObject:cancelBtn];  

        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];  
        [barItems addObject:flexSpace];  

        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];  
        [barItems addObject:doneBtn];  


        [keyboardDoneButtonView setItems:barItems animated:YES]; 
        textField.inputAccessoryView = keyboardDoneButtonView;

        textField.inputView = pickerView;


    return YES;
}


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


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    if (txtFieldSelected == @"firstTF") {
        return ary1.count;
    }else if (txtFieldSelected == @"secondTF") {
        return ary2.count;
    }
    return 1;

}

-(void)doneButtonPressed:(id)sender{  
    if (txtFieldSelected == @"firstTF") {
        [firstTF resignFirstResponder];

    }else if (txtFieldSelected == @"secondTF") {

        [secondTF resignFirstResponder];
    }
}  

-(void)cancelButtonPressed:(id)sender{ 

    if (txtFieldSelected == @"firstTF") {
        [firstTF resignFirstResponder];

    }else if (txtFieldSelected == @"secondTF") {
        [secondTF resignFirstResponder];

    }
}  


- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // called when a row is selected

    if (txtFieldSelected == @"firstTF") {

        firstTF.text = [ary1 objectAtIndex:row];
    }else if (txtFieldSelected == @"secondTF") {
        secondTF.text = [ary2 objectAtIndex:row];

    }

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (txtFieldSelected == @"firstTF") {
        return [ary1 objectAtIndex:row];
    }else if (txtFieldSelected == @"secondTF") {
        return [ary2 objectAtIndex:row];
    }

    return @"Good Day";
}
于 2013-02-19T15:00:53.260 に答える
0
   textField1.inputView = pickerView1;
   textField2.inputView = pickerView2;

   pickerView.tag = 1;
   pickerView.tag = 2;

UIPickerViewタグでデリゲート メソッドを使用します。

于 2013-02-19T15:35:01.113 に答える