xcode 如何在一个视图中为多个文本字段使用一个 UIPickerView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11612460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:59:51  来源:igfitidea点击:

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

iphonexcodeuitextfielduipickerview

提问by Sanjyot Shah

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 different textfields?

我在一个视图中有 8 个文本字段。我想使用选择器视图填充每个。我可以有 1 个选择器视图,它可以为不同的文本字段显示来自不同数组的不同项目列表吗?

Can someone explain how do I do it programmatically with a sample code?

有人可以解释我如何使用示例代码以编程方式进行操作吗?

Also how can I hide the pickerview when the view loads and it should be displayed only when I click on a textfield with its corresponding data list? It should disappear when I select the data and reappear when I click on a different textfield with its corresponding data list and so on.

另外,如何在视图加载时隐藏选择器视图,并且仅当我单击带有相应数据列表的文本字段时才应显示它?当我选择数据时它应该消失,并在我单击具有相应数据列表的不同文本字段时重新出现,依此类推。

I am new to xcode. Any help will be much appreciated. Thank you.

我是 xcode 的新手。任何帮助都感激不尽。谢谢你。

回答by Natan R.

You can use only one PickerView that shows different data depending on some conditions.

您只能使用一个 PickerView 根据某些条件显示不同的数据。

My idea is the following: you could create, lets say, 8 different arrays - one for populating the UIPickerView depending on what UITextField was tapped. Declare them in the interface as NSArray and initialize in viewDidLoad:

我的想法如下:你可以创建,比如说,8 个不同的数组 - 一个用于根据 UITextField 被点击的内容填充 UIPickerView。在接口中将它们声明为 NSArray 并在 viewDidLoad 中初始化:

array1st = ...
array2nd = ...
array3d = ...
//and until 8.

Then you create another just to point the array that should fill it (like NSArray *currentArray) and you don't even need to init it = it will be used only for pointing the correct array.

然后您创建另一个只是为了指向应该填充它的数组(如NSArray *currentArray),您甚至不需要初始化它 = 它仅用于指向正确的数组。

So you should set the delegate of the UITextFields to your view controller, and in the method textFieldShouldBeginEditingyou check who is the UITextField, assign the correct array as the currentArray, reload the UIPickerView with reloadData, and return NO in the same textFieldShouldBeginEditingmethod. currentTextField is a pointer only to know what is the currentUITextField being "edited" - we need to store it to be able to set the text after selecting data in the picker. Declare currentTextField in the interface.

因此,您应该将 UITextFields 的委托设置为您的视图控制器,并在textFieldShouldBeginEditing您检查 UITextField 的方法中,将正确的数组分配为 currentArray,使用 重新加载 UIPickerView reloadData,并在相同的textFieldShouldBeginEditing方法中返回 NO 。currentTextField 是一个指针,仅用于知道正在“编辑”的 currentUITextField 是什么 - 我们需要存储它以便能够在选择器中选择数据后设置文本。在界面中声明 currentTextField 。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    currentTextField = textField;
    if (textField == myFirstTextView)
    {
        currentArray = array1st;
        [pickerView reloadData];
        [self animatePickerViewIn];
        return NO;
    }
    //and this way until 8th
}

So, when your view controller, that in fact is the UIPickerViewDelegate, you populate your UIPickerView according to the current array and you don't need to change anything, just use the currentArray as the array to get data from.

因此,当您的视图控制器实际上是 UIPickerViewDelegate 时,您会根据当前数组填充 UIPickerView 并且不需要更改任何内容,只需使用 currentArray 作为从中获取数据的数组即可。

Now, to show the pickerView:

现在,显示pickerView:

After connecting the IBOutlet, in your viewDidLoad you should do two things: set the frame of the UIPickerView and add it as a subview:

连接 IBOutlet 后,在你的 viewDidLoad 中你应该做两件事:设置 UIPickerView 的框架并将其添加为子视图:

pickerView.frame = CGRectMake(0,self.view.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height);
pickerView.delegate = self;
pickerView.dataSource = self;
//or you can do it via Interface Builder, right click the pickerView and then connect delegate and dataSource to the file's owner
[self.view addSubview:pickerView];

Now you need to create the method called animatePickerViewIn, that we called when the user taps the UITextField.

现在您需要创建名为 的方法animatePickerViewIn,我们在用户点击 UITextField 时调用该方法。

-(void)animatePickerViewIn
{

    [UIView animateWithDuration:0.25 animations:^{
        [pickerView setFrame:CGRectMake(0, pickerView.frame.origin.y-pickerView.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height)];
    }];
}

To load the data in the pickerView:

要在 pickerView 中加载数据:

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

    return [currentArray count];
}


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

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [currentArray objectAtIndex:row];
}

And when the user selects the row in the pickerView, you will receive it as a delegate method. So just set the text of the currentTextField as the value selected:

当用户在 pickerView 中选择行时,您将收到它作为委托方法。所以只需将 currentTextField 的文本设置为选定的值:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //and here you can do in two ways:
    //1
    [currentTextField setText:[currentArray objectAtIndex:row]];
    //2
    [currentTextField setText:[self pickerView:pickerView titleForRow:row inComponent:component]];
}

回答by Vaghani.Rahul

Use This.

用这个。

 - (void)textFieldDidBeginEditing:(UITextField *)textField{
   MyPickervie.delegate=self;
 }
 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myLocationPickerView;
{
  return 1;
 }

-(void)myLocationPickerView:(UIPickerView *)myLocationPickerView didSelectRow:           (NSInteger)row inComponent:(NSInteger)component;
 {

    switch(textfield.tag){
     Case 0:
            label.text=[arraNo objectAtindex:row];
            break;
     default: break;
  }
 }
 -(NSInteger)myLocationPickerView:(UIPickerView *)myLocationPickerView  numberOfRowsInComponent:(NSInteger)component;
{

switch(textfield.tag){
     Case 0:
            return [arrayNo count];
            break;
     default: break;
  }
}

-(NSString *)myLocationPickerView:(UIPickerView *)myLocationPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
 {
   switch(textfield.tag){
     Case 0:
            return [arrayNo objectAtIndex:row];
            break;
     default: break;
  }

}

Where you have to take 8 Cases in switch condition to call different array and set it into the pickerView.

您必须在 switch 条件下使用 8 个 Cases 来调用不同的数组并将其设置到 pickerView 中。

回答by Hemang

I have tested this code with 3 textfields.

我已经用 3 个文本字段测试了这段代码。

And found perfect solution of your problem.

并为您的问题找到了完美的解决方案。

1) Take one global UITextField.

1) 取一个全局 UITextField。

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

2) 在文本域编辑的委托方法上,将全局文本域设置为当前文本域。

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

3)在选择器值更改方法上,设置全局文本字段的文本,它将更改所选文本字段的值。

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

回答by iPhone Developer

1.Use a toolBar with Done barButtonItem,which should hide the pickerView.

1.使用带有 Done barButtonItem 的工具栏,它应该隐藏选择器视图。

2.Now give your textFields tagValues like : textFiled1.tag=100;then using this tag value populate your pickerView array.

2.现在给你的textFields tagValues像:textFiled1.tag=100;然后使用这个标签值填充你的pickerView数组。

3.When the user selects any Value from pickerView, in its delegate method write

3.当用户从pickerView中选择任意一个Value时,在其委托方法中写入

if(textField.tag== @"Your Tag Value")
{  
      textField.text=[pickerArray objectAtIndex:row];
}

I guess you can try the code yourself, now that you have the gist of the solution.Good Luck!!

我想您可以自己尝试代码,现在您已经掌握了解决方案的要点。祝您好运!!