我只是想知道在iOS上创建应用程序。
我发现了这个:
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
变量和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
答案 0 :(得分:1)
您正在使用一个您没有的XCode版本的示例。
XCode的更高版本将为您自动合成属性,旧版本不会,它们只会抛出错误。
您需要添加
行@syntheisze label = _label;
就在看起来像@implementation HelloWorldViewController
的行之后。
虽然我想更好的解决方案只是升级XCode:)