1

如何在不使用as 的UIPickerViews情况下在 IOS中创建多个。UITextfields input accessoryViewUIPickerView

4

2 回答 2

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

希望对你有帮助

于 2013-08-29T07:08:19.237 回答
0

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

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

于 2013-09-26T11:28:59.640 回答