我是新手,我正在尝试制作一个简单的温度转换应用程序。这里有错误被抛出:
2014-02-28 20:35:46.954 Temperature Converter[5899:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<TemperatureConverterViewController 0x8a939a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key convertTemperatureButton.'
*** First throw call stack:
(
0 CoreFoundation 0x017395e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014bc8b6 objc_exception_throw + 44
2 CoreFoundation 0x017c96a1 -[NSException raise] + 17
3 Foundation 0x0117d9ee -[NSObject(NSKeyValueCoding) setValue:forUndefinedKey:] + 282
4 Foundation 0x010e9cfb _NSSetUsingKeyValueSetter + 88
5 Foundation 0x010e9253 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
6 Foundation 0x0114b70a -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 412
7 UIKit 0x004cca15 -[UIRuntimeOutletConnection connect] + 106
8 libobjc.A.dylib 0x014ce7d2 -[NSObject performSelector:] + 62
9 CoreFoundation 0x01734b6a -[NSArray makeObjectsPerformSelector:] + 314
10 UIKit 0x004cb56e -[UINib instantiateWithOwner:options:] + 1417
11 UIKit 0x0033d605 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
12 UIKit 0x0033ddad -[UIViewController loadView] + 302
13 UIKit 0x0033e0ae -[UIViewController loadViewIfRequired] + 78
14 UIKit 0x0033e5b4 -[UIViewController view] + 35
15 UIKit 0x002669fd -[UIWindow addRootViewControllerViewIfPossible] + 66
16 UIKit 0x00266d97 -[UIWindow _setHidden:forced:] + 312
17 UIKit 0x0026702d -[UIWindow _orderFrontWithoutMakingKey] + 49
18 UIKit 0x0027189a -[UIWindow makeKeyAndVisible] + 65
19 UIKit 0x00224cd0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
20 UIKit 0x002293a8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
21 UIKit 0x0023d87c -[UIApplication handleEvent:withNewEvent:] + 3447
22 UIKit 0x0023dde9 -[UIApplication sendEvent:] + 85
23 UIKit 0x0022b025 _UIApplicationHandleEvent + 736
24 GraphicsServices 0x036e02f6 _PurpleEventCallback + 776
25 GraphicsServices 0x036dfe01 PurpleEventCallback + 46
26 CoreFoundation 0x016b4d65 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
27 CoreFoundation 0x016b4a9b __CFRunLoopDoSource1 + 523
28 CoreFoundation 0x016df77c __CFRunLoopRun + 2156
29 CoreFoundation 0x016deac3 CFRunLoopRunSpecific + 467
30 CoreFoundation 0x016de8db CFRunLoopRunInMode + 123
31 UIKit 0x00228add -[UIApplication _run] + 840
32 UIKit 0x0022ad3b UIApplicationMain + 1225
33 Temperature Converter 0x00002eed main + 141
34 libdyld.dylib 0x01d7770d start + 1
35 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
现在,我知道我的按钮功能有问题所以这是我的界面和实现:
#import <UIKit/UIKit.h>
@interface TemperatureConverterViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *temperatureField;
@property (weak, nonatomic) IBOutlet UILabel *conversionResult;
- (IBAction)convertToFahrenheit:(id)sender;
@end
继承实施(.m文件)
#import "TemperatureConverterViewController.h"
@interface TemperatureConverterViewController ()
@end
@implementation TemperatureConverterViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)convertToFahrenheit:(id)sender {
double celsius = [_temperatureField.text doubleValue];
double fahrenheit = (celsius+32)*1.8;
NSString *result = [[NSString alloc] initWithFormat: @"Fahrenheit: %f",fahrenheit];
_conversionResult.text = result;
}
@end
有人能找到这个问题吗?我不能:(
谢谢。
UPDATE !!!
当我按下虚拟设备中的文本字段时,键盘显示,但按下按钮时它不会消失!有谁知道我怎么解决这个问题?
非常感谢你!
答案 0 :(得分:1)
检查XIB或Storyboard上的连接,确保所有内容都已正确连接。我打赌你联系了一个IBOutlet,你后来从你的界面中删除了仍然在文件所有者连接中闲逛的。
要解决第二个问题并解除键盘,请在IBAction中尝试以下操作:
[self.temperatureField resignFirstResponder];
答案 1 :(得分:0)
您尚未在界面构建中将UIViewController的类设置为TemperatureConverterViewControllerclass。
设置课程将解决您的问题..至少为我做了