2013-07-07 36 views
1

我正在使用在線Treehouse教程學習Objective C的過程中,我一直按照步驟進行操作,並在按下按鈕時不斷收到錯誤。我希望有人能告訴我我在這裏做錯了什麼。到目前爲止,應用程序應該在頂部有一個標籤,在按鈕被按下時在故事板中間有一個按鈕,它應該改變標籤的文本。我的代碼如下。拋出終止異常

.m文件

// 
// ViewController.m 
// Prediction 
// 
// Created by Brandon Brown on 7/7/13. 
// Copyright (c) 2013 Brandon Brown. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize predictionLabel; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [self setPredictionLabel:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (IBAction)buttonPressed:(id)sender { 
    self.predictionLabel.text = @"Yep"; 
} 
@end 

.h文件中

// 
// ViewController.h 
// Prediction 
// 
// Created by Brandon Brown on 7/7/13. 
// Copyright (c) 2013 Brandon Brown. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
- (IBAction)buttonPressed:(id)sender; 
@property (strong, nonatomic) IBOutlet UILabel *predictionLabel; 

@end 

這是錯誤即時得到控制檯

2013-07-07 17:20:09.717 Prediction[14457:f803] -[UIView setText:]: unrecognized selector sent to instance 0x6a44690 
2013-07-07 17:20:09.719 Prediction[14457:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setText:]: unrecognized selector sent to instance 0x6a44690' 
*** First throw call stack: 
(0x14b0022 0xeb0cd6 0x14b1cbd 0x1416ed0 0x1416cb2 0x22cf 0x14b1e99 0x1614e 0x160e6 0xbcade 0xbcfa7 0xbc266 0x3b3c0 0x3b5e6 0x21dc4 0x15634 0x139aef5 0x1484195 0x13e8ff2 0x13e78da 0x13e6d84 0x13e6c9b 0x13997d8 0x139988a 0x13626 0x1f0d 0x1e75) 
terminate called throwing an exception 
+0

對於OP:從這一切,你可以看到使用Interface Builder和/因爲它有很多學習曲線很容易出錯。我通常建議初學者學習如何通過編程的方式製作UI,並在第一學習階段進行練習 - 值得了解你在做什麼以及「底層」發生了什麼,而不是盲目地弄清楚什麼。 – 2013-07-07 22:35:42

+0

請仔細閱讀:'reason:' - [UIView setText:]:無法識別的選擇器發送到實例' –

回答

1

你的錯誤是說,setText方法被送往到UIView,而不是UILabel。因此,看起來您可能意外地將IBOutletUIView相關聯,而不是UILabel(如果您手動編寫IBOutlet行,這很容易實現)。因此,進入Interface Builder,在UIView(可能位於主視圖中)的「網點檢查器」(右側的最後一個選項卡)中刪除IBOutlet鏈接,並將標籤的IBOutlet鏈接起來。

順便問一下,你能避免這種混亂,如果你從手工編寫不要說IBOutlet代碼,而是控制從標籤到.h文件在助理編輯(和Interface Builder -drag會寫IBOutlet代碼行,如果你不小心抓錯了控件,它會很容易識別)。請參閱Xcode用戶指南中的Make Connections Directly Between User Interface Objects and Your Source Files