1

How to create multiple UIPickerViews in IOS without using UITextfields input accessoryView as the UIPickerView.

Kumar KL
  • 15,315
  • 9
  • 38
  • 60

2 Answers2

0

take 3 textfields and give them tag as 1,2,3 respectively. and set its delegate as well. please do the following in .h file

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

do the following in .m file

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

hope this will help you

hpp
  • 619
  • 3
  • 13
0

The best way is to create separate UIPickers in XIB file, one for each, the key is to put it in a UIView and set input accessory view of the filed to that view.

3 fields would require three pickers with their respective UIView Containers.