Reputation: 3433
I need to create two UIPickers Dynamically with different input values.
I did n't find correct solution in google.
Can any pls post some code.
Thank u in advance.
Upvotes: 2
Views: 4484
Reputation: 23722
There are two approaches to this.
a) First, make the view controller the delegate and data source of both the pickers. Then in the view controller's implementation do something like this:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ( pickerView == picker1 ? 2 : 3 );
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSArray *values = ( pickerView == picker1 ? values1 : values2 );
return [values objectAtIndex: row];
}
Note that you compare the pickerView parameter to an instance variable pointing to one of your picker views and decide "on the fly" which values to return for each of the picker views.
b) Assign different data sources and delegates to each of the picker views (they may be any objects implementing UIPickerViewDelegate and UIPickerViewDataSource protocols, not necessarily the view controller).
Upvotes: 4