2012-04-18 37 views
0

我見過很多類似的問題,但他們都不能解決我的問題,或者讓我明白這個解決方案。我用UIPickerView製作了一個nib文件。 當我運行應用程序時,我得到這個錯誤。UIPickerView NSUnknownKeyException

Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<UIApplication 0x68633e0> setValue:forUndefinedKey:]: 
this class is not key value coding-compliant for the key pickerview.' 

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 

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 

我確信,機械手的dataSourcedelegate網點都指向文件的所有者也pickerview引用插座。

UPDATE

整個堆棧跟蹤

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是誰懷疑有什麼問題的人的文件。 我會很感激。

+0

閱讀** ** TexhZen的後此鏈接 http://stackoverflow.com/questions/3088059/this-class-is-not-key-value-coding-compliant-for上鍵 – iDhaval 2012-04-18 13:56:15

+0

這將如何幫助我,我只有一個名爲'ViewController'的UIViewController',並且xib文件在檢查器中顯示了正確的名稱。我還檢查了'IBOutlets'的名稱,我看不到哪裏不對了。 – 2012-04-18 14:06:35

回答

1

進入到項目的TestPlist-Info.plist文件,並刪除此行中:didFinishLaunchingWithOptions:方法

Main nib file base name  ViewController 

這是不正確,你不會因爲它是在你的應用程序都需要處理這個參考。

+0

謝謝。你太棒了!你能解釋一下爲什麼會發生這種情況嗎?我做錯了什麼?我的代碼不正確或應用程序的設置? – 2012-04-19 11:58:55

+0

我不是100%確定這是怎麼發生的誠實 - 它永遠不會在那裏。 – InsertWittyName 2012-04-20 18:32:18

+0

好的,謝謝! – 2012-04-21 10:36:14

0

我對IB用戶並不是很多,但是不應該將屬性定義爲IBOutlet,而不是屬性本身?

@property (retain,nonatomic) IBOutlet UIPickerView *pickerview; 

而且你爲什麼不使用ARC內存管理 - 它簡化了很多東西(沒有更多的保留和釋放),所以如果你沒有不使用它,你應該切換到ARC一個特別的理由。

+0

這是一樣的,無論如何它不能解決我的問題,它仍然拋出異常。我沒有使用ARC,因爲我正在閱讀的這本書很舊,並且以這種方式管理內存 – 2012-04-18 13:44:43

+0

是否將您的nib文件所有者的類設置爲您的ViewController(在Identity Inspector中)? – TheEye 2012-04-18 13:49:04

+0

是的,File's Inspector - > Identity顯示與File name相同的名稱。 – 2012-04-18 13:56:06