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];
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];
}
I'm trying to do a double picker, one part of text and the other with images. But the code gives me an error: Thread 1: Program received signal: "EXC_BAD_ACCESS". I can't see the trouble. Here is the code, Array content Images and Array2 content text. Thanks.
#synthesize Array, picker, Array2;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIImage *one = [UIImage imageNamed:#"6-12AM.png"];
UIImageView *oneView = [[UIImageView alloc] initWithImage:one];
NSArray *Array1 = [[NSArray alloc] initWithObjects:oneView, nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"Array"];
[self setValue:Array1 forKey:fieldName];
Array2 = [[NSArray alloc] initWithObjects:#"Hello", #"trouble", nil];
[super viewDidLoad];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 2;// giving number of components in PickerView
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [self.Array2 count];// counting the number of rows in each component
}
#pragma mark Picker Delegate Methods
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
{
if (component == 1) {
return [self.Array objectAtIndex:row];
}
} //In this line is where the error occurs
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
if (component == 0) {
return [self.Array2 objectAtIndex:row];
}
}
Your error is due to the fact that you do not offer an alternative to the if statement. A non-void function must return something in every case, so you need to provide an else statement in each one of your two functions that returns a value (like nil).
I dont see you allocating or initializing Array. Hence the bad access. Did you mean to use Array1?
Also several things:
Memory allocated several places never deallocated. Leaks all over
Variable names should start with small caps like array1, array2
[self setValue:Array1 forKey:fieldName]; //what is this doing?
I made a simple picker that displays the information chose in a label on the same page. The picker is just showing ? ? ?, but when i scroll over the ? it shows the correct things chosen in the label. How do I get it to also show the words in the pickerwheel itself?
Without seeing your code, it is difficult to track down. How are you setting the information in the Label and the Picker?
If you have an array of strings which are also displayed in the Picker, then this is easy:
In the .h file, use:
#interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
NSArray * arrayOfString;
UILabel * theLabel;
UIPicker * thePicker;
}
In the .m file, use:
#pragma mark -
#pragma mark View
- (void)viewDidLoad;{
[super viewDidLoad];
arrayOfStrings = [[NSArray alloc] initWithObjects:#"1", #"2", #"etc", nil];
}
#pragma mark -
#pragma mark Picker Delegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView; {
return [arrayOfStrings count];
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component; {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; {
return [arrayOfStrings objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; {
theLabel.text = [arrayOfStrings objectAtIndex:row];
}
You then hook everything up in the nib, and this should fix your problem. Hope this helps!
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];
}