1

I have 2 UIPickerView right now and I want to add three more later.The code is below:

-(void)viewDidLoad{
Pos_x = 5;
Pos_y = 70;

for (i= 0; i<2; i++) {

myPickerView = [[UIPickerView alloc] init];
myPickerView.frame = CGRectMake(Pos_x,Pos_y,150,300);
myPickerView.tag = i;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];

if (i==0) {
// Init the data array.
Array_1 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_1 addObject:@"One"];
[Array_1 addObject:@"Two"];
[Array_1 addObject:@"Three"];
[Array_1 addObject:@"Four"];
[Array_1 addObject:@"Five"];
}

if (i==1) {

Array_2 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_2 addObject:@"One1"];
[Array_2 addObject:@"Two1"];
[Array_2 addObject:@"Three1"];
[Array_2 addObject:@"Four1"];
[Array_2 addObject:@"Five1"];  
}

Pos_x = Pos_x + 162;
}
}

I have successfully created two UIPickerView with tag. Now the problem is adding data in UIPickerView.

I am doing like this

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

//return [Array_1 objectAtIndex: row];  // If give only this line both UIPickerView show same value.

//How to show both 
//return [Array_2 objectAtIndex: row]; 

// I also tried this code

if(pickerView == myPickerView){

 if (i==0){
 }
 return [Array_1 objectAtIndex: row];
 }

 if (i==1){
 }
 return [Array_2 objectAtIndex: row];
 }


}

Now my question is that how to show different different data on two UIPickerView? like on pickerview.tag ==0 it will show return [Array_1 objectAtIndex: row]; And pickerview.tag ==1 it will show return [Array_2 objectAtIndex: row];? Any idea or suggestions would be welcome.

Afreen Khan
  • 175
  • 1
  • 4
  • 12

3 Answers3

2

Please check this code:

-(void)viewDidLoad{
myPickerView = [[UIPickerView alloc] init];
myPickerView.frame = CGRectMake(5,70,150,300);
//myPickerView.tag = i;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];

Array_1 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_1 addObject:@"One"];
[Array_1 addObject:@"Two"];
[Array_1 addObject:@"Three"];
[Array_1 addObject:@"Four"];
[Array_1 addObject:@"Five"];
[Array_1 addObject:@"Six"];


myPickerView_2nd = [[UIPickerView alloc] init];
myPickerView_2nd.frame = CGRectMake(167,70,150,300);
//myPickerView_2nd.tag = i;
myPickerView_2nd.delegate = self;
myPickerView_2nd.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView_2nd];

Array_2 = [[NSMutableArray alloc] init];

// Add some data for demo purposes.
[Array_2 addObject:@"One1"];
[Array_2 addObject:@"Two1"];
[Array_2 addObject:@"Three1"];
[Array_2 addObject:@"Four1"];
[Array_2 addObject:@"Five1"];
[Array_2 addObject:@"Six1"];

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
if([pickerView isEqual: myPickerView]){
// return the appropriate number of components, for instance
return 1;
}

if([pickerView isEqual: myPickerView_2nd]){
// return the appropriate number of components, for instance
return 1;
}
else 
return 0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {


if ( pickerView == myPickerView) // this is otherPickerview
{
    return [Array_1 count];


}
if ( pickerView == myPickerView_2nd){
    return [Array_2 count];


}
else{
     NSUInteger numRows = 5;
    return numRows;}

}

Hope This will Help You...

Samina Shaikh
  • 300
  • 2
  • 3
  • 14
0

You can use an Array or Dictionary to store the content arrays and get them with picker's tag. E. g.

NSArray *contentArrays = [[NSArray alloc] initWithObjects:Array_1, Array_2, nil];

Then get the content array with [contentArrays objectAtIndex:pickerView.tag];

iDroid
  • 10,403
  • 1
  • 19
  • 27
0

You can do so by setting tags of the pickerview:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) { 
    if(pickerView.tag == 1)
     {
          // do something with picker one
     }
     else if(pickerView.tag == 2)
     {
          // the other picker
     }
}

Or else:

   - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) { 
        switch(pickerView.tag){
           case 1:
               //do something
               break;
           case 2:
               //do other thing
               break;
           default:
               break;
        }
    }

Hope this helps..

Have a look at this as well: Multiple PickerViews in one View?

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276