为文本字段创建多个UIPickerView,而不将它们放在IOS的附件视图中

时间:2013-08-29 06:06:07

标签: ios uipickerview

如何在不使用UIPickerViews作为UITextfields input accessoryView的情况下在IOS中创建多个UIPickerView

2 个答案:

答案 0 :(得分:0)

取3个文本字段,分别标记为1,2,3。并设置其委托。 请在.h文件中执行以下操作

UIPickerView *packerView1;
    UIPickerView *packerView2;
    UIPickerView *packerView3;
    NSMutableArray *dataArray1;
    NSMutableArray *dataArray2;
    NSMutableArray *dataArray3;

在.m文件中执行以下操作

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

    [packerView1 removeFromSuperview];
    [packerView2 removeFromSuperview];
    [packerView3 removeFromSuperview];

    if(textField.tag==1)
    {
        packerView1=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
        packerView1.delegate=self;
        packerView1.dataSource=self;
        packerView1.tag=1;
        [self.view addSubview:packerView1];
    }
    else if(textField.tag==2)
    {
        packerView2=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
        packerView2.delegate=self;
        packerView2.dataSource=self;
        packerView2.tag=2;
        [self.view addSubview:packerView2];
    }
    else
    {
        packerView3=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
        packerView3.delegate=self;
        packerView3.dataSource=self;
        packerView3.tag=3;
        [self.view addSubview:packerView3];
    }

    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

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

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(pickerView.tag==1)
    {
        return [dataArray1 count];
    }
    else if(pickerView.tag==2)
    {
        return [dataArray2 count];
    }
    else
    {
        return [dataArray3 count];
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(pickerView.tag==1)
    {
        return [dataArray1 objectAtIndex:row];
    }
    else if(pickerView.tag==2)
    {
        return [dataArray2 objectAtIndex:row];
    }
    else
    {
        return [dataArray3 objectAtIndex:row];
    }
}

希望这会对你有所帮助

答案 1 :(得分:0)

最好的方法是在XIB文件中创建单独的UIPickers,每个文件夹一个,关键是将它放在UIView中,并将字段的输入附件视图设置为该视图。

3个字段需要三个拾取器及其各自的UIView容器。