How do I add multiple components to a PickerView? - iphone

It may be a simple question but how do I add multiple components to a UIPickerView? I use NSMutableArray to populate one component but I dont know how to populate the others. I also need to change the value of a label when a row is selected.
Thanks in advance
Kieran

I take it that you are a beginner. Here's how to implement the methods nacho has rightly pointed out:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
// return the number of components required
return 2;
}
You can use other NSMutableArray's to populate the components. Assuming that you have 2 components, each using different NSMutableArray i.e. array1 and array2:
// Return row count for each of the components
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [array1 count];
}
else {
return [array2 count];
}
}
// Populate the rows of the Picker
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Component 0 should load the array1 values, Component 1 will have the array2 values
if (component == 0) {
return [array1 objectAtIndex:row];
}
else if (component == 1) {
return [array2 objectAtIndex:row];
}
return nil;
}
Use this code to change the text of a label, say selectedValue when a value is selected in the PickerView:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedValue.text = [NSString stringWithFormat: #"%#, %#", [array1 objectAtIndex:[myPicker selectedRowInComponent:0]],[array2 objectAtIndex:[myPicker selectedRowInComponent:1]]];
}
... and you are good to go :)
You might also want to see http://joshhighland.com/blog/2009/09/17/uipickerview-spinning-multiple-components/ for valuable tips on pickerView programming.

You need to set the datasource and use
– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:
set its delegate and use:
– pickerView:didSelectRow:inComponent:
to change your label when a certain row in a certain component is selected. Very likely to UITableViewDelegate and Datasource
and also taking a glance to documentation helps :)

Instead of component I found using tag more proper.
Then in the code:
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//NSLog(#"Component_1: %d",pickerView.tag);
if (pickerView.tag == 1)
return [self.arrOpponentTeams count];
else if (pickerView.tag == 2)
return [self.arrMyTeams count];
else
return [self.arrPlayers count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//NSLog(#"Component_2: %d",pickerView.tag);
if( pickerView.tag == 1 )
return [self.arrOpponentTeams objectAtIndex:row];
else if (pickerView.tag == 2)
return [self.arrMyTeams objectAtIndex:row];
else
return [self.arrPlayers objectAtIndex:row];
}

I do not know if it's a little late But I solved this another way Array are a bit rudimentary But it works wonders You can even put different color to each array
- (void) viewDidLoad {
horPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..24];
minPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..59];
secPickerArray = #[#"00",#"01",#"02",#"03",#"04",#"05".to..59];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
return _HorPickerArray.count ;
}
else if (component == 1) {
return _MinPickerArray.count ;
}
else if (component == 2) {
return _SecPickerArray.count ;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [_HorPickerArray objectAtIndex:row];
}
else if (component == 1) {
return [_MinPickerArray objectAtIndex:row];
}
else if (component == 2) {
return [_SecPickerArray objectAtIndex:row];
}
return nil;
}
And Print in TextField
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
_OficialTime.text =[NSString stringWithFormat: #"%#:%#:%#", [_HorPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:0]],[_MinPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:1]], [_SecPickerArray objectAtIndex:[_TimerPiker selectedRowInComponent:2]]];
}

Here is the swift3 code.Here I am assigning values of multiple picker view to a UITextField.
var pickOption = [["1", "2", "3","4","5","6","7","8","9"], ["0","1","2", "3","4","5","6"],["0","1","2", "3","4","5","6"]]
#IBOutlet weak var travellersTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView()
pickerView.delegate = self
travellersTextField.inputView = pickerView
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return pickOption.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[component][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let adult = pickOption[0][pickerView.selectedRow(inComponent: 0)]
let child = pickOption[1][pickerView.selectedRow(inComponent: 1)]
let infant = pickOption[2][pickerView.selectedRow(inComponent: 2)]
travellersTextField.text = adult + " adults," + child + " children," + infant + " infants"
travellersTextField.becomeFirstResponder()
}

Related

Setting data for multiple UIPickerView

I have two UIPickerViews With data being pulled from an array though I can't seem to program them separately. Here is the code that I am using for my UIPickerViews:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [treatments count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[treatments objectAtIndex:row]valueForKey:#"treatmentName"];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
_buttonText.text = [[treatments objectAtIndex:row]valueForKey:#"treatmentName"];
if (![_buttonText.text isEqual: #"Pick a Treatment Name"]) {
_buttonText.textColor = [UIColor blackColor];
}
}
-(NSInteger)nursePicker:(UIPickerView *)nursePicker numberOfRowsInComponent:(NSInteger)component {
return [nurses count];
}
- (NSString *)nursePicker:(UIPickerView *)nursePicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[nurses objectAtIndex:row]valueForKey:#"nurseName"];
}
When I run the code the pickers show the same data
Thanks in advance
Store a reference to your two picker views and use the UIPickerView that is passed as an argument to the datasource methods in order to determine which picker view you are using.
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == self.nursePicker) {
return [nurses count];
}
else if (pickerView == self.treatmentPicker) {
return [treatments count];
}
}
Same idea for every datasource method
You should put a BOOL to check the data. Create a BOOL called treatment and when the first picker is available set the BOOL to YES and when the second is available set the BOOL to NO. Then check in your methods to see which picker is their and put in the data.
#interface NotesViewController ()
{
BOOL treatment;
}
- (void)firstPickerComesUP
{
treatment = YES;
}
- (void)secondPickerComesUP
{
treatment = NO;
}

UIpickerview containing 2 component, second should cotain 1component minus selected row ios

pickerview containing 2 component both having same data when I select first component then the selected component should not appear in second component
After you make a selection in the first component, remove the item from the datasource for the second component & reload the picker components. HTH.
Your question is not clear but on the basic of what i understood:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
string = [fistPickerArray objectAtIndex:row];
//do what ever logic you want
for(NSString str in secondArray)
{
if([string isEqualToString:str])
{
[secondPickerArray removeObject:str],
}
}
[picker reloadAllComponent];
}
write below code in didSelectRow method
if(component == 0) {
string = [yourarray objectAtIndex:row];
}
then in titleForRow method,
if(component == 1) { if([string isEqualToString:[yourarray objectAtIndex:row]]) { break; return nil; else{ return string; }
}
}

How to create multiple uipickerview in tableview?

PickerView delegate method...
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (didSelectedRow ==1)
{
strChkDistanceValue = [arrDistance objectAtIndex:row];
}
else
{
strChkDateRangeValue = [arrDateRange objectAtIndex:row];
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if(didSelectedRow ==1)
{
return [arrDistance count];
}
else
{
return [arrDateRange count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if(didSelectedRow ==1)
{
strChkarray =#"arrDistance";
return [arrDistance objectAtIndex:row];
}
else
{
strChkarray =#"arrDateRange";
return [arrDateRange objectAtIndex:row];
}
}
above code I am use...
I am using the pickerview in tableview... there are two picker views...
when i am use the first picker view the first picker view array shows in picker when i am select 3rd row in picker view after i select the 2nd picker view then show the second array list but second array list show start with 3rd row...same to 1st picker...
but i want to show at first row of array in pickerviw..
please help me...
Thanks & regards
Rahul Virja
You should use below method of UIPickerView to select a particular row.
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

shows warning msg at the end of the UIPickerViewDelegate method

I have created a pickerview in my application and create two component of it for that in my .h file code is as follows
#interface tweetViewController : UIViewController<UIPickerViewDataSource ,UIPickerViewDelegate> {
NSArray *activities;
NSArray *feelings;
}
#property(nonatomic,retain) NSArray *activities;
#property(nonatomic,retain) NSArray *feelings;
#end
after that in my .m file i have implemented compulsory method for UIPickerViewDataSource and UIPickerViewDelegate but both compulory methods of UIPickerViewDatasource works fine
and its code is
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return [activities count];
}
else
{
return [feelings count];
}
} return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [activities count];
}
else
{
return [feelings count];
}
}
but the method of UIPickerViewDelegate shows warning at the end of the method
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0:
return [activities objectAtIndex:row];
break;
case 1:
return [feelings objectAtIndex:row];
break;
}
}
tell me why does the warning occur????
The warning "control may reach end of non-void function.." means to say that the method is returning nothing as the method is having some return value.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *strToReturn = #"";
switch (component) {
case 0:
strToReturn = [activities objectAtIndex:row];
break;
case 1:
strToReturn = [feelings objectAtIndex:row];
break;
}
return strToReturn;
}
Modify your code as above and the warning will be no more to irritate you.
//numberOfRowsInComponent must return NSInteger value
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [activities count];//then wont execute feelings count
}//if loop failed then ur feeling count
return [feelings count];
}
//here u can remove else loop with same meaning with your context
for titleForRow method u have to return nsstring in atleast in default case
so specify defaust case for some some string

How can I get multiple arrays into UIPickerview

I have an problem in UIPickerview, it is displayed in this screenshot.
Screenshot of my UIPickerView
I have three arrays. Each array is passed into UIPickerview. I can display the three columns in UIPickerview.
My problem is that the arrays are not passed into each column and are not passed into the sub-columns.
How can I solve the problem? Please help me.
In UIPickerView delegate:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent (NSInteger)component
check for
if(component == 0)
{
//use Array1
}
else if(component == 1)
{
//use Array2
}
else if(component == 2)
{
//use Array3
}