Ios 带有错误的简单应用程序";此类不符合密钥“的键值编码”;

Ios 带有错误的简单应用程序";此类不符合密钥“的键值编码”;,ios,xib,key-value-coding,Ios,Xib,Key Value Coding,我用xib文件创建了一个简单的iPhone应用程序,并在视图中添加了一个按钮。创建一个IBOutlet来连接它。每次我启动它,它都会崩溃。完整的错误消息如下所示: 2014-05-03 08:10:19.742测试[1435:a0b]*由于未捕获异常而终止应用程序 “NSUnknownKeyException”,原因:“[setValue:forUndefinedKey:]:此类不符合密钥txtBtn的键值编码。” 有很多人在问这个问题,在回顾了答案之后,我认为我的问题是不同的。 代码如下 #i

我用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
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>

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

您需要从此行更改:

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

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

您还需要在AppDelegate中导入XIBViewContoller.h。

整个错误消息是什么?它应该说它抱怨的是哪把钥匙。用更多信息更新帖子。谢谢。这个错误通常是因为你搞砸了XIB到类的映射——你把XIB链接到了一个不包含
txtBtn
方法的类。(编译器是否警告您没有合成txtBtn?)我发布了一个答案,应该可以解决这个问题。。。但出于好奇,既然您显然是在启动一个新项目,而不是维护遗留代码,为什么不使用故事板?来学习新东西。在xib文件中,我已经指定文件所有者是XIBViewController,为什么系统不能返回XIBViewController对象?然后我不导入头文件,因为您的代码告诉它创建一个类型为
UIViewController
的对象,并且它的视图应该由您指定的nib名称创建。指定文件所有者只是为了将xib文件中的出口连接到适当的类。
XIBViewController *controller = [[XIBViewController alloc] initWithNibName:@"XIBViewController" bundle:[NSBundle mainBundle]];