如何在一个视图中使用一个UIPickerView用于多个文本字段? [英] How to use one UIPickerView for multiple textfields in one view?

查看:69
本文介绍了如何在一个视图中使用一个UIPickerView用于多个文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

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

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

推荐答案

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

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

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

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:

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

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

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.

所以你应该将UITextFields的委托设置为你的视图控制器,并在方法 textFieldShouldBeginEditing 你检查谁是UITextField,将正确的数组指定为currentArray,使用 reloadData 重新加载UIPickerView,并在相同的 textFieldShouldBeginEditing 方法。 currentTextField是一个指针,只知道被编辑的currentUITextField是什么 - 我们需要存储它以便能够在选择器中选择数据后设置文本。在接口中声明currentTextField。

So you should set the delegate of the UITextFields to your view controller, and in the method textFieldShouldBeginEditing you check who is the UITextField, assign the correct array as the currentArray, reload the UIPickerView with reloadData, and return NO in the same textFieldShouldBeginEditing method. 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.

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

因此,当你的视图控制器,实际上是UIPickerViewDelegate,你根据当前数组填充你的UIPickerView,你不需要改变任何东西,只需使用currentArray作为数组来获取数据。

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.

现在,要显示pickerView:

Now, to show the pickerView:

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

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

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

现在你需要创建名为 animatePickerViewIn ,当用户点击UITextField时我们调用。

Now you need to create the method called animatePickerViewIn, that we called when the user taps the 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)];
    }];
}

要在pickerView中加载数据:

To load the data in the 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];
}

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

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:

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

这篇关于如何在一个视图中使用一个UIPickerView用于多个文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆