本文介绍了有错误的简单应用程序此类对于键“不符合键值编码".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 xib 文件创建了一个简单的 iPhone 应用程序,并在视图中添加了一个按钮.创建一个 IBOutlet 与之连接.每次,我启动它,它会崩溃.完整的错误信息如下:2014-05-03 08:10:19.742 test[1435:a0b] * 由于未捕获的异常而终止应用

'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: 此类不符合键值 txtBtn 的键值编码.'

有很多人问这个问题,在查看答案后,我认为我的问题是不同的.代码如下.

#import @interface XIBViewController : UIViewController{UIButton *txtBtn;}@property (nonatomic, 保留) IBOutlet UIButton *txtBtn;@结尾

创建此视图控制器的来源:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//在应用程序启动后覆盖自定义点.self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];UIViewController *controller = [[UIViewController alloc] initWithNibName:@"XIBViewController" bundle:[NSBundle mainBundle]];self.window.rootViewController = 控制器;返回是;}

xib 文件的来源是

<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/><button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizo​​ntalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="kvg-9r-q01"><rect key="frame" x="103" y="158" width="79" height="30"/><autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/><state key="normal" title="Hello World"><color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/></状态></subviews><color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/><simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/><simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/></查看></对象></文档>
解决方案

您已经创建了一个 UIViewController 对象,它没有 txtBtn 属性.>

您需要从这一行更改:

UIViewController *controller = [[UIViewController alloc] initWithNibName:@"XIBViewController" bundle:[NSBundle mainBundle]];

为此:

XIBViewController *controller = [[XIBViewController alloc] initWithNibName:@"XIBViewController" bundle:[NSBundle mainBundle]];

您还需要在 AppDelegate 中#import XIBViewContoller.h.

I create a simple iPhone app with xib file, and add one button to the view. create a IBOutlet to connect with it. each time, I launch it, it will crash. the full error message is as below:2014-05-03 08:10:19.742 test[1435:a0b] * Terminating app due to uncaught exception

'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key txtBtn.'

There are many people hitting this question, After reviewing the answers, I think my problem is different one.code is as below.

#import <UIKit/UIKit.h>

@interface XIBViewController : UIViewController
{
    UIButton *txtBtn;
}
@property (nonatomic, retain) IBOutlet UIButton *txtBtn;
@end

source of creating this view controller:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UIViewController *controller = [[UIViewController alloc] initWithNibName:@"XIBViewController" bundle:[NSBundle mainBundle]];
    self.window.rootViewController = controller;

    return YES;
}

source of xib file is

<?
解决方案

You've created a UIViewController object, which doesn't have a txtBtn property.

You need to change from this line:

UIViewController *controller = [[UIViewController alloc] initWithNibName:@"XIBViewController" bundle:[NSBundle mainBundle]];

To this:

XIBViewController *controller = [[XIBViewController alloc] initWithNibName:@"XIBViewController" bundle:[NSBundle mainBundle]];

You'll also need to #import XIBViewContoller.h in your AppDelegate.

这篇关于有错误的简单应用程序此类对于键“不符合键值编码".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:46