类不符合键值编码 [英] Class is not key value coding-compliant

查看:132
本文介绍了类不符合键值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个错误的含义,但我真的很挣扎,我需要别人的帮助:

I know the meaning of this error, but I'm really struggling with it, and I need someone's help :

2010-09-21 15:03:11.562 Stocks[5605:207] *** Terminating app due to uncaught 
exception 'NSUnknownKeyException', reason: '[<NSObject 0x499fb20> 
setValue:forUndefinedKey:]: this class is not key value coding-compliant 
for the key actionText.'

这里有我的代码:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface AlertCell : UITableViewCell {
    IBOutlet UILabel *actionText;
}

@property (retain, nonatomic) UILabel *actionText;

@end

@implementation AlertCell
@synthesize actionText;

- (void)dealloc {
    [actionText release];
    [super dealloc];
}

@end

问题恰好发生在:

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    AlertCell *cell = 
      (AlertCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AlertCell" 
                                                         owner:nil 
                                                       options:nil];
        for (id oneObject in nib) {
            if ([oneObject isKindOfClass:[UITableViewCell class]]) {
                cell = (AlertCell *)oneObject;
                break;
            }
        }
    }    


    cell.actionText.text = [arrayAlert objectAtIndex:indexPath.row];
    return cell;
}

在这一行:

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AlertCell" 
                                             owner:nil 
                                           options:nil];

根据要求,这是我的TableViewCOntroller标题:

As asked, here is my header for the TableViewCOntroller :

#import <UIKit/UIKit.h>


@interface AlertesViewController : UITableViewController {
    NSMutableArray *arrayAlert;
}

你可以看到我的XIB文件(作为XML): http://pastebin.com/FDVzLYZu

And you can see my XIB file (as XML): http://pastebin.com/FDVzLYZu

@end

任何人都可以帮助我吗?非常感谢!

Can anyone help me ? Thanks a lot !

推荐答案

有几个选项可以解决这个问题 - 我会让你决定哪个是最合适的。

There are a couple of options to resolve this - i'll let you decide which is the most appropriate.

它失败的原因是因为所有者被传递为零。您将actionText插座绑定到IB中文件的所有者,但是在加载笔尖时,所有者为零。我猜想当在幕后加载一个nil所有者时会使用NSObject,这就是你得到键/值错误的原因。

The reason it's failing is because the owner is being passed as nil. You're binding the actionText outlet to the file's owner in IB, but then when loading the nib, the owner is nil. I'd guess that when loading with a nil owner behind the scenes an NSObject is used, which is why you're getting the key/value error.

我之前的建议由于我不知道Nib是如何构建的,因为所有者也会失败而传递单元格;因为你还没有创建它,所以单元格为零(并且dequeue正在向后传递nil,所以即使传递单元格,因为所有者仍然基本上没有通过nil)。

My previous advice to pass the cell as the owner would also fail as I didn't know how the Nib is constructed; the cell is nil as you've yet to create it (and dequeue is passing nil back, so even pass cell as the owner is still essentially passing nil).

两个选项:


  • 在-cellForRowAtIndexPath :( NSIndexPath *)indexPath实现中实例化一个新单元格,并将该新单元格作为所有者传递(但是我猜这不是最好的解决方案。)

  • 或者,我建议这是更好的解决方案,将你的nib文件中actionText的绑定更改为Alert Cell而不是文件的所有者(你有文件的所有者和一个警报单元 - 将UILabel绑定到Alert Cell的actionText出口,而不是File的所有者,这就是目前正在做的事情) - 我怀疑这就是你想。考虑到这一点,文件的所有者可以再次成为NSObject。

-------原始答案保存在文件的所有者之下class也是导致此错误的常见原因-------

------- Original answer kept below as the file's owner class is also a common cause for this error -------

它表明你已经在InterfaceBuilder中实例化了一个AlertCell,并且你正在绑定to action to actiontext,但该类未设置为AlertCell,它仍然是NSObject?

It suggests that you've 'instantiated' an AlertCell in InterfaceBuilder, and you're binding something to actiontext, but the class isn't set to AlertCell, it's still NSObject?

查看工具选项板的标识选项卡上的类文本框Interface Builder中的该对象。这个类应该是AlertCell,但我猜它仍然设置为NSObject。

Take a look at the class text box on the identify tab of the tool palette for that object in Interface Builder. The class should be AlertCell, but i'd guess it's still set to NSObject.

顺便说一句,并且可以随意忽略这个建议,但是有几个我鼓励你做的额外的事情,纯粹来自 Objective C期望/约定的观点:

As an aside, and feel free to ignore this advice, but there are a couple of extra things i'd encourage you to do, purely from an Objective C expectations/conventions point of view:


  • 在课后命名您的文件(大写文件名的第一个字符)。

  • 在您的班级名称前加上;两个大写字符,通常是您的姓名缩写(例如,我将其命名为DWAlertCell)。

这篇关于类不符合键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆