1

我正在尝试在 IO 中创建我的第一个测试应用程序,而我所做的一切都会让我遇到这个错误。我正在使用 xcode 4.4。

该应用程序非常简单。它有一个按钮,当我按下它时,必须出现一个标签和一个图像视图。

我的整个代码是这样的:

ViewController.h


//
//  ViewController.h
//  helloWorld_04
//
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    IBOutlet UILabel *label;
    IBOutlet UIImageView *Kant;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UIImageView *Kant;
- (IBAction)buttonGuess:(id)sender;

@end

和我的实现文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize label,Kant;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [label release];
    [Kant release];
    [super dealloc];
}
- (IBAction)buttonGuess:(id)sender {
    label.text=@"Hello World i am back!";
    UIImage *imageSource=[UIImage imageNamed:@"kantStair.png"];
    Kant.image=imageSource;
}
@end

我的错误日志是这样的:

2012-08-23 13:38:50.030 helloWorld_04[537:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x6a5e1f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key image.'
*** First throw call stack:
(0x14b2022 0xeb2cd6 0x14b1ee1 0x9c3022 0x934f6b 0x934edb 0x94fd50 0x23771a 0x14b3dea 0x141d7f1 0x23626e 0xdc1fc 0xdc779 0xdc99b 0x3b401 0x3b670 0x3b836 0x4272a 0x2d1b 0x13386 0x14274 0x23183 0x23c38 0x17634 0x139cef5 0x1486195 0x13eaff2 0x13e98da 0x13e8d84 0x13e8c9b 0x13c65 0x15626 0x2a22 0x2995)
terminate called throwing an exception(lldb) 

它让我在那条线上发出信号:

//
//  main.m
//  helloWorld_04
//

//

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

我在这里阅读了几个答案,但没有找到解决我的问题的方法,这就是为什么我把它变成一个新问题。我认为这可能是连接问题,所以我自己做的是将我的标签和图像“darg and drop”给“file's owner”,但仍然出现错误。

4

2 回答 2

3

查看您的错误消息,您似乎image在 Interface Builder 中定义了某些内容,但不存在。例如,也许你有类似的东西:

示例 IB 屏幕

但你没有image财产。您曾经有过一个吗,也许已将其重命名为Kant?如果您在 Interface Builder 中定义了类似的内容,请将其删除(通过点击我突出显示的插座旁边的“x”),然后将其再次链接到您的新控件。


更新:

您绝对应该修复上面的错误,希望上面的观察可以帮助您找到它。您的代码中没有错误,但问题无疑在于您的 Interface Builder 链接。

但是,话虽如此,如果你是一个新程序员,我希望你不介意一些不请自来的风格观察。显然,鉴于这是一个风格问题,这些都可以辩论,但我认为这些都代表了既定或新兴的 iOS 编码标准。无论如何,我将来可能会建议:

  1. 就像 David H 建议的那样,您应该使用小写的变量名。

  2. 您可能应该有这样的@synthesize语句@synthesize label = _label,或者,如果您使用的是 Xcode 4.4 或更高版本,则完全省略该@synthesize语句。(我知道 Interface Builder 可以@synthesize为您生成一个简单的语句,但最好的做法是@synthesize使用唯一的实例变量名称,这样您就不会意外地将实例变量与属性混淆。)

  3. 正如 Born Survivor 所指出的,您应该在init和方法中引用您的实例变量dealloc(即带前导下划线的变量名),并在其他地方使用属性(带前导)。self.

  4. 您可能应该省略实例变量并让@synthesize语句为您执行这些操作(这样如果您打错字,就不会意外地得到两个实例变量)。

因此,您的代码将变为:

//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// note, no braces with instance variables defined
// once upon a time that was recommended by Apple, but no longer
// just let the following @property statements and the subsequent
// @synthesize statement generate the instance variables for you.

@property (retain, nonatomic) IBOutlet UIImageView *kant; // note the lower case "k"
@property (retain, nonatomic) IBOutlet UILabel *label;

- (IBAction)buttonGuess:(id)sender;

@end

//  ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize label = _label;  // note the @synthesize statement let's us define what the instance variable name should be, _label in this case
@synthesize kant  = _kant;

- (void)viewDidUnload
{
    [self setKant:nil];
    [self setLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [_kant release];  // note I'm now using the instance variables that begin with the underscore
    [_label release];
    [super dealloc];
}

- (IBAction)buttonGuess:(id)sender
{
    self.label.text = @"Hello World i am back!";  // note, I'm using the properties with the preceding "self."
    UIImage *imageSource = [UIImage imageNamed:@"kantStair.png"];
    self.kant.image = imageSource;
}

@end
于 2012-08-23T15:55:19.983 回答
0

在 ObjectiveC 中,总是用小写字母命名变量很重要——因为当你合成像“foo”这样的变量时,你会得到“-(id)foo;// getter”和“-(void)setFoo:(id) val;//二传手”。看看 setter 如何使用大写字母。因此,您应该做的第一件事是将“Kant”重命名为“kant”。[按照惯例,只有类的首字母大写,所以你可以帮助像我这样的其他人按照惯例阅读你的代码。]

因此,您要做的第一件事是将“Kant”更改为“kant”,然后返回界面构建器视图并将新命名的变量重新连接到 UIImageView。

如果这不能解决问题,那么您已经连接了“kant”或者发生了其他一些奇怪的事情 - 在设置图像之前添加这行代码:

NSLog(@"kant has class %@", NSStringFromClass([kant class]) );

让我们看看它到底是什么。

于 2012-08-23T11:49:31.637 回答