2014-10-27 42 views
0

我對iOS編程真的很陌生。我只是試圖讓一個按鈕單擊事件,當我運行模擬器異常出現按鈕點擊事件的NSUnknownKeyException

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

+0

請[在錯誤上搜索](http://stackoverflow.com/search?q=NSUnknownKeyException+this+class+is+not+key+value+coding-compliant+for+the+key)張貼。這可能是最常發佈的前3個問題之一。 – rmaddy 2014-10-27 17:15:20

回答

2

你必須擁有無效的連接三種不同的看法。如果你去查看你的viewcontrollers的連接檢查器,你會看到button1,label1和textField1是無效的。那些被列爲!

在這裏看到firgure。 enter image description here

如果通過單擊X按鈕從連接檢查器中刪除button1,label1和textField1連接,則應該很好。

+0

我剛剛刪除了那些!現在當我點擊按鈕,NSInvalidArgumentException出現 – 2014-10-27 17:20:57

+0

好吧我解決了這個問題。 – 2014-10-27 17:24:29

3

異常消息告訴所有人。您的Storyboard/IB連接設置爲不存在的名爲button1的插座。您的房產被命名爲button

要糾正這一點,你將需要刪除您的故事板或XIB文件斷開的鏈接,並重新連接您的UIButton的引用出口button