首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIPickerView NSUnknownKeyException

UIPickerView NSUnknownKeyException
EN

Stack Overflow用户
提问于 2012-04-18 13:07:53
回答 2查看 838关注 0票数 0

我见过很多类似的问题,但都没有解决我的问题,也没有向我说明解决办法。我用UIPickerView制作了一个nib文件。当我运行应用程序时,我会得到这个错误。

代码语言:javascript
复制
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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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引用插座。

更新

整个堆栈跟踪

代码语言:javascript
复制
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) 

这里是为任何想知道问题所在的人提供的文件。我会非常感激的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-18 18:50:49

进入项目中的TestPlist-Info.plist文件并删除以下行:

代码语言:javascript
复制
Main nib file base name     ViewController

这是不正确的,您不需要这个引用,因为它都是在您的应用程序:didFinishLaunchingWithOptions: method中处理的。

票数 1
EN

Stack Overflow用户

发布于 2012-04-18 13:30:45

我不是一个IB用户,但是您不应该将属性定义为IBOutlet,而不是属性本身吗?

代码语言:javascript
复制
@property (retain,nonatomic) IBOutlet UIPickerView *pickerview;

为什么不使用ARC进行内存管理--这大大简化了事情(不再保留和发布),因此,如果您没有特定的原因不使用它,您应该切换到ARC。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10210036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档