-1

我只是想知道在 iOS 上创建应用程序。

我找到了这个:

http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/chapters/RM_YourFirstApp_iOS/Articles/01_CreatingProject.html#//apple_ref/doc/uid/TP40011343-TP40012323-CH3-SW3

Apple 的入门小教程。

你看,在最后完成整个教程之后,我尝试编译我的项目,当项目即将显示时出现了这个错误:

2012-09-03 10:05:46.201 HelloWorld[29361:f803] *** Terminating app due to uncaught    exception 'NSUnknownKeyException', reason: '[<HelloWorldViewController 0x6837c10>      setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
*** First throw call stack:
(0x13c7022 0x1558cd6 0x13c6ee1 0x9bf022 0x930f6b 0x930edb 0x94bd50 0x23371a 0x13c8dea         0x13327f1 0x23226e 0xd81fc 0xd8779 0xd899b 0x37401 0x37670 0x37836 0x3e72a 0xf596 0x10274     0x1f183 0x1fc38 0x13634 0x12b1ef5 0x139b195 0x12ffff2 0x12fe8da 0x12fdd84 0x12fdc9b 0xfc65     0x11626 0x1c3d 0x1ba5)
terminate called throwing an exception

我的猜测是我做错了连接,但是什么?

我什至复制粘贴了他们给你的最后一个 .m 和 .h ,但它仍然无法编译。

属性“变量”需要定义方法“setLabel” - 使用@synthetize

variable 和 setLabel 更改了 6 次(不同的警告)。

我究竟做错了什么?

--------编辑这是我的viewcontroller.h

#import "HelloWorldViewController.h"

@interface HelloWorldViewController ()

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel 
*label;

- (IBAction)changeGreeting:(id)sender;

@end

@implementation HelloWorldViewController

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

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

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

- (IBAction)changeGreeting:(id)sender {

self.userName = self.textField.text;

NSString *nameString = self.userName;
if ([nameString length] == 0) {
    nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
self.label.text = greeting;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

if (theTextField == self.textField) {
    [theTextField resignFirstResponder];
}
return YES;
}

@end
4

1 回答 1

1

您正在使用您没有的 XCode 版本的示例。

更高版本的 XCode 会自动为您合成属性,旧版本不会,它们只会抛出错误。

您需要添加该行

@syntheisze label = _label;

就在看起来像的行之后@implementation HelloWorldViewController

虽然我想一个更好的解决方案只是升级 XCode :)

于 2012-09-03T15:07:22.327 回答