按钮单击事件的NSUnknownKeyException

时间:2014-10-27 17:08:05

标签: ios objective-c nsunknownkeyexception

我是iOS编程的新手。我只是想做一个按钮点击事件,当我运行模拟器时出现异常

2014-10-27 12:00:00.859 practiceapp[682:28288] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7f95fb721120> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button1.'

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextField *textfield;
@property (weak, nonatomic) IBOutlet UIButton *button;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (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)changebuttonaction:(id)sender {

    NSString *st = self.textfield.text;
    self.label.text=st;
    [self.textfield resignFirstResponder];
}



@end

您可以在此处找到来源:https://github.com/liaoxsong/practiceapp

2 个答案:

答案 0 :(得分:3)

异常消息告诉所有人。您的Storyboard / IB连接设置为名为button1的插座,该插座不存在。您的媒体资源名为button

要解决此问题,您需要删除故事板或xib文件中的断开链接,然后将UIButton的引用插座重新连接到button

答案 1 :(得分:2)

您有三种不同的视图,其连接无效。如果您查看viewcontrollers的连接检查器,您将看到button1,label1和textField1无效。这些被列为!。

在这里仔细查看一下。 enter image description here

如果您通过单击X按钮从连接检查器中删除button1,label1和textField1连接,那么您应该很高兴。