Wednesday, April 18, 2012

UIPickerView NSUnknownKeyException

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 picker view referencing outlet.





No comments:

Post a Comment