删除故事板并使用Xib生成错误“类不是密钥的密钥值编码”

问题描述:

我最近在Xcode上编写了一个简单的应用程序,目的是学习如何编写iPhone程序。我编写的程序使用xib文件。由于我使用的是Xcode5,因此我删除了故事板并添加了xib文件。然后,在项目的Info属性中,将变量“Main storyboard file base name”更改为“Main nib file base name”,并将该值设置为我的xib文件的名称。后来,在文件的所有者中,我将自定义类设置为QuizViewController(我的xib的控制器)并进行了所有必要的连接(IBAction和IBOutlet)。删除故事板并使用Xib生成错误“类不是密钥的密钥值编码”

我没有任何编译错误,但是当我执行的项目,我碰到下面的错误

慢速英语12:37:17.872测验[1616:70B] ***终止应用程序由于未捕获异常'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:该类不是关键值campoPreguntas的密码值。

我不明白这个错误的含义。

我QuizViewController.h的代码是这样

@interface QuizViewController : UIViewController 
{ 
    int indicePreguntaActual; 

    //objetos del modelo 
    NSMutableArray *preguntas; 
    NSMutableArray *respuestas; 

    //objetos de la vista 
    IBOutlet UILabel *campoPreguntas; 
    IBOutlet UILabel *campoRespuestas; 
} 

- (IBAction)mostrarPregunta:(id)sender; 
- (IBAction)mostrarRespuesta:(id)sender; 

@end 

而对于我QuizViewController.m代码

#import "QuizViewController.h" 

@interface QuizViewController() 

@end 

@implementation QuizViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
//llamamos al init implementado en la superclase 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

if (self) { 
    //creamos dos arreglos y hacemos que los punteros apunten a ellos 
    preguntas = [[NSMutableArray alloc]init]; 
    respuestas = [[NSMutableArray alloc]init]; 

    //añadimos las preguntas y respuesas a los arreglos 
    [preguntas addObject:@"Cuanto es 7 + 7"]; 
    [respuestas addObject:@"14"]; 

    [preguntas addObject:@"Cuanto es 5 + 5"]; 
    [respuestas addObject:@"10"]; 

    [preguntas addObject:@"Cuanto es 2 + 2"]; 
    [respuestas addObject:@"4"]; 
} 

//retornamos la direccion del nuevo objeto 
return self; 
} 

-(IBAction)mostrarPregunta:(id)sender 
{ 
//incrementamos el indice de las preguntas 
indicePreguntaActual++; 

//verificamos que el indice no sea mayor al tamaño del arreglo y si es igual 
//al valor maximo hacemos que el indice valga 0 para iniciar nuevamente 
if (indicePreguntaActual == [preguntas count]) { 
    indicePreguntaActual = 0; 
} 

//obtenemos la pregunta en el indice 
NSString *pregunta = [preguntas objectAtIndex:indicePreguntaActual]; 

//mostramos la pregunta en la interfaz 
[campoPreguntas setText:pregunta]; 

//limpiamos el campo respuesta 
[campoRespuestas setText:@"???"]; 

//escribimos la pregunta que se muestra en el log 
NSLog(@"mostrando la pregunta: %@",pregunta); 
} 

-(IBAction)mostrarRespuesta:(id)sender 
{ 
//obtenemos la respuesta 
NSString *respuesta = [respuestas objectAtIndex:indicePreguntaActual]; 

//mostramos la respuesta 
[campoRespuestas setText:respuesta]; 

} 

@end 

我的连接看起来像这样

Connections

感谢您的时间和您的帮助。

+0

http://*.com/a/5458188/3186896它应该是有帮助的。 – Robert

+0

[该类不是关键的givenName的键值编码兼容]的可能重复(http://*.com/questions/5458129/this-class-is-not-key-value-coding-compliant-for-在琴键给定名称) – Joel

它通常是因为你有一些在IB中连接的东西,你删除了代码