Setting data for multiple UIPickerView - iphone

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

Related

UIPickerView with empty arrays

I've set up a UIPickerView successfully, but I'm wondering about the best way to handle the case when the data is initially empty. For my current iteration I use the following code for the pickerdelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([flash_cards count])
{
return [[flash_cards allKeys] objectAtIndex:row];
}
return #"";
}
where
flash_cards = NSMutableDictionary *
Whenever I try to scroll it creates an error of indexoutofbounds which is only logical. How do I handle the case of an empty array?
EDIT:
Also as part of the code I implemented the following
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponen: (NSInteger)component
{
return [flash_cards count];
}
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
To solve this problem you should return 1 instead 0 in the method numberOfRowsInComponent from the UIPickerViewDataSource when the array is empty, like the example below:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.myArray count] == 0 ? 1 : [self.myArray count];
}
You should also implement the UIPickerViewDataSource methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
You can still use flash_cards (or [flash_cards count]) to derive the return values of these methods; if your data is empty, you can return 0, then later call -reloadAllComponents on your picker view when you get data loaded.

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;

Iphone:Split a string seperated by comma and print each in row of pickerview

I got a string as mild,medium,hot.I split the string with comma as separator.I need to print it in a pickerView too.
I used the following code and got List count as 3 successfully.
NSString *spList=[mdict objectForKey:#"spicinesstype"];
NSArray *list = [spList componentsSeparatedByString:#","];
NSLog(#"List count:%d",[list count]);
return [list count];
But how could I display all 3 items in pickerview
You should set your class as the delegate of the picker view, Then implement these 3 delegate methods for your picker
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
First you need to implement UIPickerViewDelegate and UIPickerViewDataSource by putting them on the end of the #interface line of the header file of your view controller.
Like this: #interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
Next you need to set your view controller as the delegate and data source of you picker view. You can either do this in the - (void)viewDidLoad method of your view controller by adding the lines:
myPickerView.delegate = self;
myPickerView.dataSource = self;
Or you can link it up in Interface Builder if you are using that.
Then you need to implement these delegate methods in your View Controller source file.
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// whatever you want to happen when a row is selected.
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [list count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [list objectAtIndex:row];
}

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

UIpickerView populating multiple uitextFields in the same ScrollView

Hi I am developing an iphone app where I have populated a uipickerview and it works great with one uitextfield. But I have 6 uitextfield I need to populate and I am using the same uipickerview for each UItextfield.
I have working the Ipickerview loaded with array objects and it pops up when each field is touched. The problem is with the code below the UItextfields share the same data from the picker.
I can not figure out how to code so each field gets it own data from the UIPickerView row.
What Am I doing wrong? Any coding suggestions?
Thanks
#implementation BestViewController
-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)thePickerView
{ return 1; }
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
{return [list count]; }
-(NSString *)pickerView:(UIPickerView *)thePickerview titleForRow:(NSInteger)row forComponent:(NSInteger)component
{return [list objectAtIndex:row]; }
-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
uitextfield1.text = [list objectAtIndex:row];
utitextfield2.text = [list objectAtIndex:row];
}
- (void)viewDidLoad {
[super viewDidLoad];
uitextfield1.inputView = pickerView;
uitextfield2.inputView = pickerView;
list = [[NSMutableArray alloc] init];
[list addObject:#"a"];
[list addObject:#"b"];
[list addObject:#"c"];
[list addObject:#"d"];
}
You need to get hold of the current first responder and set its text property rather than explicitly setting a particular text field.
So,
-(void)pickerView:(UIPickerView *)thePickerview didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
// textFields is an NSArray holding each of the textfields that are using the picker as an input view
for (UITextField textField in textFields)
{
if ([textField isFirstResponder])
{
textField.text = [list objectAtIndex:row];
break;
}
}
}
There may be a more elegant way to find the current first responder, this is off the top of my head. textFields could possibly be an IBOutletCollection, but I haven't used those myself so I can't speak with much authority.
Here is how I would do it:
First, define several different pickers. Of course you use the same UIPickerView but you change one property that helps you distinguish them. There is different data for (almost) each text field. One convenient property that Apple designed exactly for this purpose is the tag, an arbitrary integer available to every UIView. You can assign the same tags to the UITextFields.
For example:
#define kFirstTextField 101
#define kSecondTextField 102
#define kThirdTextField 103
//... etc
In the method when the text field is touched:
[myPickerView setHidden:NO]; // or however you show the picker
[myPickerView setTag:textField.tag];
[myPickerView reloadAllComponents];
In the data methods of the picker:
-(NSString *)pickerView:(UIPickerView *)thePickerview
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (thePickerView.tag==kFirstTextField)
{ return [list1 count]; }
if (thePickerView.tag==kSecondTextField)
{ return [anotherOrTheSameList count]; }
// of course, you can also use a switch statement
return [defaultList count];
}
Do something similar in the titleForRow: method.
Finally, when something is picked, discriminate again by tag:
-(void) pickerView:(UIPickerView *)thePickerview
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
UITextField *field = (UITextField *) [thePickerview.superview viewWithTag:thePickerview.tag];
// this assumes the UIPickerView is the subview of the same view as the UITextFields
if (thePickerview.tag==kFirstTextField)
{ field.text = [list1 objectAtIndex:row]; }
if (thePickerview.tag==kSecondTextField)
{ field.text = [anotherOrTheSameList objectAtIndex:row]; }
// etc.
// alternatively:
field.text = [self pickerView:thePickerview titleForRow:row forComponent:0];
}