iPhone SDK: NSInvalidArgumentException -UIView numberOfComponentsInPickerView - iphone

I am trying to develop a simple search mechanism on a UIPicker. The approach I am using is to keep two arrays. My problem is that for some reason I am getting this run-time error.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView numberOfComponentsInPickerView:]
Here are the array declarations.
//data source for UIPicker NSArray
*arrayCountryChoices;
//search results buffer
NSMutableArray *arraySearchResults;
//properties
#property(nonatomic,retain) NSArray*arrayCountryChoices;
#property(nonatomic,retain) NSMutableArray *arraySearchResults;
Here is where I initialize the data
//create data
arrayCountryChoices = [NSArray arrayWithObjects:#"foo",#"bar",#"baz",nil];
//copy the original array to searchable array
arraySearchResults = [arrayCountryChoices mutableCopy];
An the picker methods.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arraySearchResults count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arraySearchResults objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//grab the selected country
strUserChoice = [arraySearchResults objectAtIndex:row];
}
Here is the search code for completeness although not really relevant yet as the app dies before we ever get here.
//filter on search term
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"SELF beginswith[c] %#", strSearchText];
[arraySearchResults filterUsingPredicate: predicate];
[pickerCountry reloadComponent:0];
I have also dragged datasource and delegate connections from the UIPicker to Files Owner in Interface Builder.
Any ideas?
Thanks in advance.

Looks like you've got the picker's data source set to something other than the object that implements the code you've posted there—apparently a UIView somewhere. Make sure the picker's outlets point to your actual data-source object.

Related

How to copy NSMutableArray value into another NSMutableArray in iPhone?

Here, i have a NSMutableArray in my AppDelegate page, the array will be updated when the user add the values (user name) in the database. Now i want the array values(user name) to be displayed in the next view to be list out in the Picker View. I know there is more related questions is there, but i can't find the solution. Any help appreciated.
Here is my code:
The array i have declared in the AppDelegate page, named jobarray, now i need to copy the items in the jobarray into next view, there i have declared pickerarray.
pickerarray = [[NSMutableArray alloc] initWithArray:appDelegate.jobarray copyItems:YES];
But it returns error message,
['NSInvalidArgumentException', reason: '-[Coffee copyWithZone:]: unrecognized selector sent to instance 0x822d410'] where 'Coffee' is my sqlite3 object file.
Your SQLite object does not conform to the NSCopying protocol - you haven't implemented the
- (id)copyWithZone:(NSZone *)zone;
method. You can either implement this method to make your object copyable, but if you only need the copying for displaying, you'd better copy the array itself only:
NSMutableArray *newArray = [originalArray mutableCopy];
this only retains the first array's objects, no need to mess with implementing the copying protocol.
[arr1 initWithObjects:#"1",#"2", nil];
makes no sense, what you probably want is to use the NSMutableArray:
NSArray *arr1 = [NSArray arrayWithObjects:#"1",#"2", nil];
NSMutableArray *arr2 = [arr1 mutableCopy];
NSLog(#"arr1: %p, %#", arr1, arr1);
NSLog(#"arr2: %p, %#", arr2, arr2);
NSLog output:
arr1: 0x7924c50, (
1,
2
)
arr2: 0x7924f00, (
1,
2
)
To populate a UIPickerView from an array you need to first create a connection for the UIPickeView Data Source and Delegate back to the ViewController. You can do this by CTRL dragging in Interface Builder in the normal way - from your UIPickerView control.
You will also need to ensure that you include the delegate protocal to your interface file. So something like this;
#ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> {
}
Then you will need to create the delegated methods for the population of the UIPickerView in your implementation file.
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return myArrayObject.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//set item per row
return [myArrayObject objectAtIndex:row];
}
This assumes that myArrayObject has been properly declared and populated elsewhere. I.e. in viewDidLoad - if content is static.

iPhone Exception throw [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
this class is not key value coding-compliant for the key
I am facing this type of problem as when I am creating an application in XCode for iphone .
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x4d2eb20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key singlePicker.'
*** Call stack at first throw:
you are setting a value for a NULL Key
Edit:
befor you start using the picker you need to:
in the .h file:
UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
NSArray *array;
}
then in the .m file:
-(void)viewDidload{
[super viewDidload];
array = [[NSArray alloc]initWithObjects:#"Lake",#"Diana",#"Jone",#"Alice",#"Byber",#"Nuces",nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [array count];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
/* do what you want if the picker selected */
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [array objectAtIndex:row];
}

UIPicker crashing

My UIPicker is crashing if the NSArray of objects is greater than 3, with the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSAutoreleasePool pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance
Here is my code for the functions:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.glassPickerOptions = [[NSArray alloc] initWithObjects:#"3mm",#"4mm",#"DG4+4",#"DG4+6",nil];
[glassPicker setFrame:CGRectMake(0, 0, 320, 162)];
[glassPicker selectRow:1 inComponent:0 animated:NO];
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSInteger glassPickerOptionsCount = self.glassPickerOptions.count;
NSLog(#"%i", glassPickerOptionsCount);
return glassPickerOptionsCount;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return (NSString*) [self.glassPickerOptions objectAtIndex:row];
}
Hopefully I havent missed anything. Thanks in advance
It seems that you overrelease your picker view, you can see this because the message is being sent to an autoreleasepool and not the object you expect, you should check out your retain/releases for your picker see whats going on, cant really tell from the code posted...

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

Very simple iPhone question. How to make UIPickerView with two columns?

I have read other questions about creating a UIPickerView with two or more columns but cannot find the exact solution.
How is this done for the iPhone programmatically? How do you add static data? Thanks!
Make your controller (or whatever is controlling the behavior of the PickerView) support the UIPickerViewDelegate protocol. Then, implement:
- (int) numberOfColumnsInPickerView:(UIPickerView*)picker
to return the number of columns you want, and
- (int) pickerView:(UIPickerView*)picker numberOfRowsInColumn:(int)col
to return the number of rows for each column, and finally:
- (UIPickerTableCell*) pickerView:(UIPickerView*)picker tableCellForRow:(int)row inColumn:(int)col
to setup each cell.
See the reference for UIPickerView and UIPickerViewDelegate.
There is an excellent tutorial on this subject here.
Assuming you have a dictionary or a couple of arrays holding your static data. For simplicity I will go with a very simple array.
You have to modify your view controllers interface definition to tell the program that your view controller can provide data and delegation to a picker view.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
Than just implementing a couple of methods will make it work however documentation should be checked for other methods that are optional but provides more customization and control.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
NSArray *options;
- (void)viewDidLoad
{
[super viewDidLoad];
options = #[#"a",#"b",#"c",#"d"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [options count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [options objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"%# selected.",[options objectAtIndex:row]);
}