【问题标题】:Deleting Storyboard and using Xib generates error "class is not key value coding-compliant for the key"删除 Storyboard 并使用 Xib 生成错误“类不是键值编码兼容键”
【发布时间】:2014-02-10 00:51:07
【问题描述】:

我最近在 Xcode 上编写了一个简单的应用程序,目的是学习如何为 iPhone 编写程序。我编写的程序使用 xib 文件。由于我使用的是 Xcode5,因此我删除了情节提要并添加了 xib 文件。然后,在项目的 Info 属性中,我将变量“Main storyboard file base name”更改为“Main nib file base name”并将值设置为我的 xib 文件的名称。后来,在 File's Owner 中,我将自定义类设置为 QuizViewController(我的 xib 的控制器)并建立了所有必要的连接(IBAction 和 IBOutlet)。

我没有任何编译错误,但是当我执行项目时出现以下错误

2014-01-20 12:37:17.872 Quiz[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

我的连接如下所示

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

【问题讨论】:

标签: ios iphone objective-c xcode


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2014-05-17
    相关资源
    最近更新 更多