UIPickerView NSUnknownKeyException [duplicate] - iphone

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 7 years ago.
I've seen a lot of similar questions but none of them solve my problem or made the solution clear to me. I've a made a nib file with a UIPickerView.
When I run the application I get this error.
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<UIApplication 0x68633e0> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key pickerview.'
My ViewController.h
#import <UIKit/UIKit.h>
#define CATEGORY 0
#define VALUES 1
#interface ViewController : UIViewController <UIPickerViewDelegate , UIPickerViewDataSource> {
NSDictionary* dictionary;
NSArray *keys,*values;
IBOutlet UIPickerView *pickerview;
}
#property (retain,nonatomic) NSArray *keys, *values;
#property (retain,nonatomic) NSDictionary *dictionary;
#property (retain,nonatomic) UIPickerView *pickerview;
#end
My ViewController.m
#import "ViewController.h"
#implementation ViewController
#synthesize keys,values,dictionary;
#synthesize pickerview;
-(void)dealloc {
[keys release];
[values release];
[dictionary release];
[pickerview release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
NSBundle* bundle = [NSBundle mainBundle];
NSString* str = [bundle pathForResource:#"testlist" ofType:#"plist"];
NSDictionary* tempd = [[NSDictionary alloc] initWithContentsOfFile:str];
self.dictionary = tempd;
[tempd release];
self.keys = [dictionary allKeys];
self.values = [dictionary objectForKey: [keys objectAtIndex:0]];
[super viewDidLoad];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == CATEGORY) {
return keys.count;
}
return values.count;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == CATEGORY) {
NSArray* tvalues = [dictionary objectForKey:[keys objectAtIndex:row]];
self.values = tvalues;
[pickerview selectRow:0 inComponent:VALUES animated:YES];//Xreiazetai?
[pickerview reloadComponent:VALUES];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == CATEGORY) {
return [keys objectAtIndex:row];
}
return [values objectAtIndex:row];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I made sure that dataSource , delegate outlets of the Picker are pointing to the File's Owner as also the pickerview referencing outlet.
UPDATE
The whole stack trace
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".Attaching to process 1236.
2012-04-18 17:07:33.444 TestPlist[1236:f803] *** Terminating app due
to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x6a68e90> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key pickerview.'
*** First throw call stack:
(0x13bb052 0x154cd0a 0x13baf11 0x9b2032 0x923f7b 0x923eeb 0x93ed60 0x23191a 0x13bce1a
0x1326821 0x23046e 0x232010 0x1214a 0x12461 0x117c0 0x20743 0x211f8 0x14aa9 0x12a5fa9 0x138f1c5
0x12f4022 0x12f290a 0x12f1db4 0x12f1ccb 0x112a7 0x12a9b 0x20a2 0x2015)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
(gdb)
Here is the file for anyone who wonders what is the problem.
I'll appreciate a lot.

Go into the TestPlist-Info.plist file in your project and remove this line:
Main nib file base name ViewController
That's incorrect, you do not need this reference as it's all handled in your application:didFinishLaunchingWithOptions: method.

I'm not much of an IB user, but shouldn't you define the property as IBOutlet, not the attribute itself?
#property (retain,nonatomic) IBOutlet UIPickerView *pickerview;
And why are you not using ARC for memory management - that simplifies things a lot (no more retain and release), so if you don't have a particular reason for not using it you should switch to ARC.

Related

Trying to display an image and label when making a selection from the UIPicker

I have an array for the geek names and an array for the careers of each geek. The geek names should appear in the UIPicker. When a name is selected, the image and the career of the selected player should be displayed at the bottom of the picker. I am unable to place the images in an array as _ImageView = #[#".... because every time I attempt it my screen goes black. When I delete it, it runs but then it crashes right after it opens on my device and I get the following message:
2013-05-02 13:40:03.761 Assignment5[2968:c07] -[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7549d30
2013-05-02 13:40:03.780 Assignment5[2968:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7549d30'
*** First throw call stack:
(0x1c92012 0x10cfe7e 0x1d1d4bd 0x1c81bbc 0x1c8194e 0xc634 0xd25f 0x642dd 0x10e36b0 0x228efc0 0x228333c 0x228eeaf 0x1032bd 0x4bb56 0x4a66f 0x4a589 0x497e4 0x4961e 0x4a3d9 0x4d2d2 0xf799c 0x44574 0x4476f 0x44905 0x4d917 0x1196c 0x1294b 0x23cb5 0x24beb 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1217a 0x13ffc 0x1e9d 0x1dc5)
libc++abi.dylib: terminate called throwing an exception
Here is my code:
ViewController.h
#import <UIKit/UIKit.h>
#interface Assignment5ViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
#property (strong, nonatomic) IBOutlet UIPickerView *picker;
#property (strong, nonatomic) IBOutlet UILabel *career;
#property (strong, nonatomic) IBOutlet UIImageView *geekImageView;
#property (strong, nonatomic)NSArray *geekNames;
#property (strong, nonatomic)NSArray *careerNames;
#end
ViewController.m
#interface Assignment5ViewController ()
#end
#implementation Assignment5ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_geekNames = #[#"Alan Turing", #"Bill Gates", #"Dennis Ritchie", #"James Gosling", #"Larry Wall", #"Linus Torvalds", #"Mark Zuckerberg", #"Steve Jobs", #"Tim Berners-Lee", #"Vinton Cerf"];
_careerNames = #[#"Conceived Turing Machine", #"Started Microsoft", #"Created C and Unix", #"Created Java Language", #"Created Perl Language", #"Created Linux OS", #"Created Facebook", #"Started Apple", #"Created World-Wide-Web", #"Created TCP/IP"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
#pragma mark
#pragma mark PickerView DataSource
- (NSInteger)numberofComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _geekNames.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_geekNames objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
_careerNames = [[NSString alloc]initWithFormat:#"%#", [_geekNames objectAtIndex:row]];
}
#end
It looks like from that error, that you've set the picker's data source to a UIView rather than your view controller. How did you set the data source? Did you do that in IB? You should delete that connection and redo it.

MKMapView - Removing annotation causes app crash

Removing annotations from my map view in the following way:
if ([[self.mapView annotations] count] > 0)
{
[self.mapView removeAnnotations:[self.mapView annotations]];
}
causes my application to crash with the following exception:
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MKAnnotationContainerView 0xe87b420> for the key path "title" from <PFAnnotation 0x10851230> because it is not registered as an observer.'
The annotations are added in the following way:
CLLocationCoordinate2D pinPosition;
for (int index = 0; index < [array count]; index++)
{
Station *aStation = [array objectAtIndex:index];
PFAnnotation *stationPin = [[PFAnnotation alloc] init]; //StationPinView
pinPosition = CLLocationCoordinate2DMake([[aStation valueForKey:#"latitude"] doubleValue], [[aStation valueForKey:#"longitude"] doubleValue]);
stationPin.stationName = [aStation valueForKey:#"stationName"];
stationPin.stationPosition = pinPosition;
stationPin.stationLength = [aStation valueForKey:#"platformLength"];
[self.mapView addAnnotation:stationPin];
[stationPin release];
}
My PFAnnotation.h is:
#interface PFAnnotation : NSObject <MKAnnotation>
{
NSString *stationName;
CLLocationCoordinate2D stationPosition;
NSNumber *stationLength;
}
#property (nonatomic, retain) NSString *stationName;
#property CLLocationCoordinate2D stationPosition;
#property (nonatomic, retain) NSNumber *stationLength;
#end
and my PFAnnotation.m is:
#implementation PFAnnotation
#synthesize stationName;
#synthesize stationPosition;
#synthesize stationLength;
- (CLLocationCoordinate2D)coordinate;
{
return stationPosition;
}
- (NSString *)title
{
return stationName;
}
- (NSString *)subtitle
{
if (stationLength == nil)
return nil;
else
return [NSString stringWithFormat:#"Platform Length: %#ft",stationLength];
}
- (void)dealloc {
[stationName release];
[stationLength release];
[super dealloc];
}
I have read in some other threads that, setting the annotation properties from background thread is the reason for the above error. But in my case,it is not so because every thing is performed on the main thread. Please advise.
ok..solved it at last!!! I think it was due to the animation provided during addition of annotations. since there were a number of annotations that were added back-to-back with animation and also the annotations were removed just before the animation started, there could have been a reference to the released annotation(this is my guess). Moreover, the removal+addition process was made on each regionDidChangeAnimated call, which could have made a overlap between the removal and addition process. Anyway, how I solved it was that, I provided a timer which will be fired only after 1 second after every regionDidchangeAnimated to make sure that the user has done with dragging. Thus unnecessary addition+removal of annotations was avoided and I was able to avoid the crash. Thanks to all guys here for their time taken to support me, especially Guntis Treulands.
In your PFAnnotation class, did you declare both title and subtitle properties as they are in the protocol?
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html
If Your PFAnnotation really has incorrect setters getters for string values:
from here: http://cocoadevcentral.com/d/learn_objectivec/
Setter:
- (void) setCaption: (NSString*)input
{
[caption autorelease];
caption = [input retain];
}
Getter:
- (NSString*) caption
{
return caption;
}
Release:
- (void) dealloc
{
[caption release];
[super dealloc];
}
also - it's easier to provide coordinates in this way: (also works on ios 3.1.3)
stationPin.stationPosition = (CLLocationCoordinate2D) {[[aStation valueForKey:#"latitude"] doubleValue], [[aStation valueForKey:#"longitude"] doubleValue]}
than (only from ios 4)
stationPin.stationPosition = CLLocationCoordinate2DMake([[aStation valueForKey:#"latitude"] doubleValue], [[aStation valueForKey:#"longitude"] doubleValue]);
Please check whether an explicit removal of observer to the property "title" is being done anywhere in the code.

Two uipicker in the same view

what i am trying to do is to put two uipickers on the same view, i read somewhere that one of them should have its own separate delegate i tried to do so but i couldn't make it work properly.
Some times when i run the application the second application just stops working with no errors on the console.
this is the .h file for the view:
#interface FirstViewController : UIViewController {
IBOutlet UIPickerView *cities;
NSMutableArray *array;
NSString *picked;
}
#property(nonatomic,retain) IBOutlet UIPickerView *cities;
#property(nonatomic,retain) IBOutlet NSMutableArray *array;
and here is the .m:
#implementation FirstViewController
#synthesize cities,array;
-(void) getCities:(NSString *)link{
url=link;
NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
if([str length]==0){
[str release];
return;
}
SBJsonParser *parser=[[SBJsonParser alloc] init];
array=[[parser objectWithString:str] copy];
[receivedData release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { // This method needs to be used. It asks how many columns will be used in the UIPickerView
return 1; // We only need one column so we will return 1.
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have.
return [array count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array.
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // what happens when selecting rows
picked=[array objectAtIndex:row];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //method asks for what the title or label of each row will be
return [array objectAtIndex:row]; // We will set a new row for every string used in the array.
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[sv setScrollEnabled:TRUE];
[sv setContentSize:CGSizeMake(320, 800)];
[self getCities:#"any url"];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
}
#end
and for the second uipicker i added an nsobject to the view and changed its class to "SecondPickerDelegate" which i created before and this is its code:
.h
#import <UIKit/UIKit.h>
#interface FirstViewSecondPickerDelegate : UIViewController<UIPickerViewDelegate>{
IBOutlet UIPickerView *specialities;
NSMutableArray *array;
NSString *picked;
}
#property(nonatomic,retain) IBOutlet UIPickerView *specialities;
#property(nonatomic,retain) NSMutableArray *array;
#property(nonatomic,retain) NSString *picker;
#end
the .m file:
#import "FirstViewSecondPickerDelegate.h"
#import "JSON.h"
#implementation FirstViewSecondPickerDelegate
#synthesize specialities,array,picker;
-(void) getSpecialities:(NSString *)link{
url=link;
NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
if([str length]==0){
[str release];
return;
}
SBJsonParser *parser=[[SBJsonParser alloc] init];
array=[[parser objectWithString:str] copy];
for(int i=0;i<[array count];i++){
NSLog(#"index %i",i);
NSLog(#"value %#",[array objectAtIndex:i]);
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { // This method needs to be used. It asks how many columns will be used in the UIPickerView
return 1; // We only need one column so we will return 1.
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { // This method also needs to be used. This asks how many rows the UIPickerView will have.
return [array count]; // We will need the amount of rows that we used in the pickerViewArray, so we will return the count of the array.
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // what happens when selecting rows
picked=[array objectAtIndex:row];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //method asks for what the title or label of each row will be
return [array objectAtIndex:row]; // We will set a new row for every string used in the array.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
picked=#"1";
[self getSpecialities:#"http://localhost:8080/Test/gs"];
}
return self;
}
/*
-(void) viewDidLoad{
[super viewDidLoad];
}
*/
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
If any one has a working example for such case please help.
You don't need separate delegates for two UIPickerView's in the same view.
Use the UIPickerView tag property, and then you can differentiate between them in delegate methods.

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.

iPhone UIPickerView in UIViewController

I have a xib in which I have added a UIViewController named delta. The view under delta is controlled by the delta viewcontroller, not the file owner. On the delta view, I have a UIViewPicker. My issue is that I am programming in the UIPickerView in the deltaviewcontroller and I'm issuing deltaviewcontroller as the delegate and data source for the UIPickerView. Everything should work, but when I load the deltaviewcontroller's view, the application crashes. If I carry out everything the same way under the file's owner view, it works fine. I'm wondering if there is really a way to make the UIPickerView work with a UIViewController and no necessarily a file's owner.
For references, the code:
Header
#interface DeltaViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
NSMutableArray *arrayNo;
IBOutlet UIPickerView *pickerView;
}
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) NSMutableArray *arrayNo;
#end
Implementation
#import "DeltaViewController.h"
#implementation DeltaViewController
#synthesize pickerView;
#synthesize arrayNo;
- (void)viewDidLoad
{
NSMutableArray *dollarsArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 100; i++)
{
NSString *item = [[NSString alloc] initWithFormat:#"%i", i];
[dollarsArray addObject:item];
}
self.arrayNo = dollarsArray;
[dollarsArray release];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arrayNo count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arrayNo objectAtIndex:row];
}
- (void)dealloc {
[arrayNo release];
[pickerView release];
[super dealloc];
}
#end
Please point out if I'm doing anything wrong. Help is much appreciated.
If you search how to add a UIPickerView in a UIView, just check out the Apple sample code for UIPickerView. I think it's one of the best ways learning to use the different classes correctly. There are five project which includes an UIPickerView. Here's the link
UIPickerView class reference and sample code
Try doing
[self.view addSubview:pickerView];
in your viewDidLoad method.