加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何在一个视图中使用一个UIPickerView用于多个文本字

发布时间:2020-12-14 20:00:52 所属栏目:百科 来源:网络整理
导读:我在一个视图中有8个文本字段.我想使用pickerview填充每个. 我可以有1个选择器视图,它将显示不同文本字段的不同数组的不同项目列表吗? 有人可以解释如何使用示例代码以编程方式执行此操作? 另外,如何在加载视图时隐藏选择器视图,只有当我单击带有相应数据
我在一个视图中有8个文本字段.我想使用pickerview填充每个.
我可以有1个选择器视图,它将显示不同文本字段的不同数组的不同项目列表吗?

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

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

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

解决方法

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

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

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

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

因此,您应该将UITextFields的委托设置为视图控制器,并在方法textFieldShouldBeginEditing中检查谁是UITextField,将正确的数组指定为currentArray,使用reloadData重新加载UIPickerView,并在相同的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
}

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

现在,要显示pickerView:

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

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

-(void)animatePickerViewIn
{

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

要在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的文本设置为所选值:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读