简单的应用程序有错误"这个类不是密钥值编码兼容的密钥"

时间:2014-05-03 15:16:45

标签: ios xib key-value-coding

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

'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:此类与密钥txtBtn不符合密钥值编码。'

有很多人在回答这个问题,在回顾完这些问题后,我认为我的问题不同了。 代码如下。

#import <UIKit/UIKit.h>

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

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

- (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;
}

xib文件的来源是

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="4510" systemVersion="12F45" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3742"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="XIBViewController">
            <connections>
                <outlet property="txtBtn" destination="kvg-9r-q01" id="Xtc-tf-xGb"/>
                <outlet property="view" destination="1" id="l1j-Dp-A65"/>
            </connections>
        </placeholder>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view contentMode="scaleToFill" id="1">
            <rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="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"/>
                    </state>
                </button>
            </subviews>
            <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
            <simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
            <simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
        </view>
    </objects>
</document>

1 个答案:

答案 0 :(得分:1)

您已创建了一个UIViewController对象,该对象没有txtBtn属性。

你需要改变这一行:

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

对此:

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

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