how to reload the picker view with new data - iphone

I am loading the picker view first time with the values which stored in array.
I am using,
temp = [NSArray arrayWithObjects:#"Male", #"Female", nil];
- (NSString *)pickerView:addData titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [temp objectAtIndex:row];
}
But it shows the empty picker view.

Did you try [somePickerVier reloadAllComponents];?
Also, the correct delegate method is - (NSString *)pickerView: (UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component.

Make sure that you set the delegate and datasource?
If not,
You have to implement this in your .h file
#interface YourView : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>

u have been using autorelease array
so u have to retain this if u want
u dont like this
then do alloc and init then
add male,female...
temp = [[NSArray arrayWithObjects:#"Male", #"Female", nil]retain];
(NSString *)pickerView:addData titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [temp objectAtIndex:row];
}

Related

Can we assign dates to the UIDatePicker

I have an array of dates ,is it possible to assign those dates as datasourse of the UIDatePicker in an iphone app ? if so, how can i do that? Thanx in advance!
UIDatePicker does not have a data source.Use UIPickerView for the purpose ,load your dates and Bang.good to go
You could try something like this:
NSString * str = [[NSString alloc]initWithFormat:#"%#",[yourDateArray objectAtIndex:0]];
yourDatePicker.date = [[NSDateFormatter alloc] init] dateFromString:str];
Hope this helps.
No you cant use a UIDatePicker like that. You can make use of UIPickerView.
use normal UIPickerView, instead of UIDatePickerView. You can create sections in UIPickerView
You will have to implement the datasource and the delegate.
once you press a button, set an array pointer to the appropriate array.
than call [thePicker reloadAllComponents];
-(IBAction) usButtonPressed:(id)sender{
self.inputArray = self.usArray;
[self.thePicker reloadAllComponents];
}
-(IBAction) mexicoButtonPressed:(id)sender{
self.inputArray = self.mexicoArray;
[self.thePicker reloadAllComponents];
}
the datasource methods:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.inputArray count];
}
the delegate methods:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.inputArray objectAtIndex:row];
}

UIPickerView not displaying data

I've followed a couple youtube tutorials as well as advice on here but I can't seem to get my UIPickerView to display its data. I'm populating the picker using three arrays and I thought I've implemented the code correctly but obviously something is wrong. The picker displays the number of elements correctly, the problem is that they are all displayed as '?'
I've tried setting the picker's delegate to self, but that didn't work either. Thanks in advance.
Here's my .h file:
#interface BACcalcViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
#property(strong, nonatomic) IBOutlet UIPickerView *personInfoPicker;
#property(strong, nonatomic) NSArray *heightsArray;
#property(strong, nonatomic) NSArray *weightsArray;
#property(strong, nonatomic) NSArray *timeArray;
#end
Here's my .m file:
#interface BACcalcViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
#end
#implementation BACcalcViewController
#synthesize personInfoPicker, heightsArray, weightsArray, timeArray;
- (void)viewDidLoad
{
[super viewDidLoad];
heightsArray = [NSArray arrayWithObjects:
#"5ft 0in",
#"5ft 1in",
#"5ft 2in",
#"5ft 3in",
#"5ft 4in",
#"5ft 5in",
#"5ft 6in",
#"5ft 7in",
#"5ft 8in",
#"5ft 9in",
#"5ft 10in",
#"5ft 11in",
#"6ft 1in",
#"6ft 2in",
#"6ft 3in",
#"6ft 4in",
#"6ft 5in",
#"6ft 6in",
#"6ft 7in",
#"6ft 8in",
nil];
weightsArray = [NSArray arrayWithObjects:
#"100",
#"110",
#"120",
#"130",
#"140",
#"150",
#"160",
#"170",
#"180",
#"190",
#"200",
#"210",
#"220",
#"230",
#"240",
#"250",
#"260",
nil];
timeArray = [NSArray arrayWithObjects:
#"1",
#"2",
#"3",
#"4",
#"5",
#"6",
nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)personInfoPicker
{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component
{
if (component == 0)
return [heightsArray count];
else if (component == 1)
return [weightsArray count];
else
return [timeArray count];
}
- (NSString *)pickerview:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
return [heightsArray objectAtIndex:row];
else if (component == 1)
return [weightsArray objectAtIndex:row];
else
return [timeArray objectAtIndex:row];
}
#end
You need to set picker view's dataSource and call reloadAllComponents after populating arrays.
self.personInfoPicker.delegate = self;
self.personInfoPicker.dataSource = self;
[self.personInfoPicker reloadAllComponents];
try initialising all three arrarys in init method.
can you try this below code.
`NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:#"01",#"02",#"03",#"04",#"05",#"06",#"07",#"08",#"09",#"10",#"11",#"12", nil];
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if ([pickerView isEqual:_month_picker_view]) {
return [_month_ary count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if ([pickerView isEqual:_month_picker_view])
{
return [_month_ary objectAtIndex:row];
}
return nil;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([pickerView isEqual:_month_picker_view]) {
_Pay_Month_txt.text=[_month_ary objectAtIndex:row];
deal_cridit_card_month_str = [_month_ary objectAtIndex:row];
}
}`
I´ve copied your initial code and found your bug now. There is a typo in the delegate method name
instead of
- (NSString *)pickerview:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
it must be
- (NSString *)pickerView:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
If you write pickerview it will declare a new method in your class which will never be called. So simple but anoying anyway :-)
i Think your code is absolutely correct and you set datasource and delegate as well but you forget to reload uipickerview so in last of viewdidload reload pickerview that solve your problem
Your code is fine. Just need to change in object of "pickerView" name in method.
Replace
- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component
By,
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
And,
- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component
By,
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
Because personInfoPicker is the object of PickerView you used already in .h file, So it'll conflict while loading data for pickerView.
Hopefully, it'll help you.
Thanks.
I think you did't allocated the array used as data source of picker e.g
NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:#"01",#"02",#"03",#"04",#"05",#"06",#"07",#"08",#"09",#"10",#"11",#"12", nil];

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

Array disappears

I'm in a view that has a UIPickerView. Here is where I'm making my sortedArray.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableArray *studentNames = [[NSMutableArray alloc] init];
for (Student *student in course.students)
[studentNames addObject:student.name];
sortedArray = [studentNames sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
[picker selectRow:(kRowMultiplier*sortedArray.count)/2 inComponent:0 animated:NO];
}
I can do an NSLog([sortedArray componentsJoinedByString:#", "]); in these two methods and it works:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
and
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
but when I do that same trace in this method it doesn't work (It crashes):
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
I don't understand why the sortedArray works everywhere but in this one method.
From documentation of sortedArrayUsingSelector::
The new array contains references to the receiver’s elements, not copies of them.
Maybe the original strings are already released?
BTW, I see that you don't release the studentNames - memory leak...

Dynamically Add values to UIPickerView at run time

How can you dynamically add values to UIPickerView at runtime.
I'm using the following code to populate a UIPickerView statically. Need to add
values dynamically at run time, for e.g. Three, Four etc.
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *title = nil;
if(row==0){
title = #"One";
}
if(row==1){
title = #"Two";
}
If you had an array of the titles, you could use something like:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [titles objectAtIndex:row];
}
The titles will be refreshed when you call [pickerView reloadAllComponents] (or reloadComponent: if you have more than one column and only want to refresh one of them).
Hey I had a problem with UIPickerView..... when I call reloadAllComponents it wasn't calling widthForComponent, but it was calling other delegate methods such as titleForRow.... I have no idea why this is, perhaps apple doesn't want the width changing dynamically? anyway it was on another screen so I didn't have the animation/no-animation issue. I found that by resetting the delegate of the picker, I could get widthForComponent to be called again!
i.e.
picker.delegate = view;
where view implements UIPickerViewDelegate.... hope this helps someone out there!