斯坦福iphone开发教程失败。为什么我的代码崩溃了? [重复]

时间:2022-09-15 18:37:54

This question already has an answer here:

这个问题在这里已有答案:

I tried following the Stanford iphone development tutorial and I got stuck because my iPhone app is crashing. Help!

我试着遵循斯坦福iphone开发教程,因为我的iPhone应用程序崩溃而陷入困境。帮帮我!

Error message

2013-03-03 12:10:39.055 RPN Calculator[2166:11303] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key digitPressed.

2013-03-03 12:10:3​​9.055 RPN计算器[2166:11303] *由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥值编码兼容的密钥digitPressed。

Header

import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display;
@end

Implementation

#import "CalculatorViewController.h"
@interface CalculatorViewController ()
@end
@implementation CalculatorViewController

- (IBAction)digitPressed:(UIButton *)sender {
      NSString *digit = [sender currentTitle];
      NSLog(@"Digit pressed = %@", digit);
}
@end

2 个解决方案

#1


4  

You probably created an IBOutlet digitPressed instead of an IBAction. So realizing this, deleted the code from your .h and .m file. But forgot to remove the connection from .xib (happened to me also when I started off). If that is the case, you can right click on your button for which the IBAction is called and delete the connection.

您可能创建了一个IBOutlet digitPressed而不是IBAction。因此,实现这一点,删除了.h和.m文件中的代码。但是忘了从.xib中删除连接(当我开始时也发生在我身上)。如果是这种情况,您可以右键单击调用IBAction的按钮并删除连接。

#2


2  

your problem might be that your action method digitPresses is not assigned properly with the UIButton defined in your .xib or storyboard and it is missing the link defined in your code, you could check that it in the connection inspector and make sure you are connecting the action with the touch up inside event

您的问题可能是您的操作方法digitPresses没有使用.xib或storyboard中定义的UIButton正确分配,并且它缺少代码中定义的链接,您可以在连接检查器中检查它并确保连接内部事件触摸的动作

#1


4  

You probably created an IBOutlet digitPressed instead of an IBAction. So realizing this, deleted the code from your .h and .m file. But forgot to remove the connection from .xib (happened to me also when I started off). If that is the case, you can right click on your button for which the IBAction is called and delete the connection.

您可能创建了一个IBOutlet digitPressed而不是IBAction。因此,实现这一点,删除了.h和.m文件中的代码。但是忘了从.xib中删除连接(当我开始时也发生在我身上)。如果是这种情况,您可以右键单击调用IBAction的按钮并删除连接。

#2


2  

your problem might be that your action method digitPresses is not assigned properly with the UIButton defined in your .xib or storyboard and it is missing the link defined in your code, you could check that it in the connection inspector and make sure you are connecting the action with the touch up inside event

您的问题可能是您的操作方法digitPresses没有使用.xib或storyboard中定义的UIButton正确分配,并且它缺少代码中定义的链接,您可以在连接检查器中检查它并确保连接内部事件触摸的动作