UIPickerView not displaying data - iphone

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

Related

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

Can't solve the double picker with images

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?

UIPickerView crashes app when scrolling past beginning or end of list

I'm new to iOS dev, so this is probably easy to fix. I have a custom view controller in which I'm adopting the protocols to control a UIPickerView in a nib. Everything works fine unless, in the iPad simulator, I scroll the picker beyond the first item in the list or the last item in the list and release. It kicks the following error:
Thread 1: Program received signal: "EXC_BAD_ACCESS"
on this line of my main.m class:
int retVal = UIApplicationMain(argc, argv, nil, nil);
Relevant code follows:
ViewController.h
#interface BirdColorViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *birdColorPicker;
NSArray *birdColors;
}
#property (nonatomic,retain) IBOutlet UIPickerView *birdColorPicker;
Viewcontroller.m
- (void)dealloc
{
[birdColorPicker release];
[super dealloc];
}
...
- (void)viewDidLoad
{
[super viewDidLoad];
birdColors = [NSArray arrayWithObjects:#"Blue",#"Yellow",#"Red",nil];
birdColorPicker.delegate = self;
birdColorPicker.dataSource = self;
}
...
#pragma mark - UIPickerViewDataSource methods
//(UIPickerView *)thePickerView
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [birdColors count];
}
#pragma mark - UIPickerViewDelegate methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [birdColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// Set value in prefs/model
}
Try:
birdColors = [[NSArray alloc] initWithObjects:#"Blue",#"Yellow",#"Red",nil];
instead of birdColors = [NSArray arrayWithObjects:#"Blue",#"Yellow",#"Red",nil];
Make birdColors a property also (nonatomic, retain) like you do with the pickerView.
Your array is not being retained, so you're accessing zombie memory.
Set NSZombieEnabled=YES in the properties/General panel of your Executable. That will tell you exactly what is being accessed.

how to reload the picker view with new data

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

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...