2012-09-25 48 views
0

我剛開始爲xcode上的ios 6開發。 但是作爲新手開發者,我遇到了一個問題。 按照第3章「開始ios5開發:探索ios sdk」一書中的指導,「按鈕有趣」示例。xcode上的未聲明標識符

我遇到了問題,我已經在.h代碼中聲明瞭標識符'statusText'。

這是我的代碼到目前爲止,任何幫助將不勝感激。先謝謝你。

我BIDViewController.h是像這樣

#import <UIKit/UIKit.h> 

@interface BIDViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel * 
statusText; 
- (IBAction)buttonPress:(UIButton *)sender; 

@end` 

和我BIDViewController.m是像這樣

#import "BIDViewController.h" 

@interface BIDViewController() 

@end 

@implementation BIDViewController 

- (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)buttonPress:(UIButton *)sender { 
    NSString *title = [sender titleForState:<#(UIControlState)#>]; 
    statusText.text = [NSString stringWithFormat:@"%@ button pressed.", title]; 
} 
@end 

我按照這本書,但似乎無法理解爲什麼發生這種情況,請幫幫我。

+0

您遇到的具體問題是什麼?編譯器問題或運行時錯誤? – tomasmcguinness

回答

2

嗯,首先,這應該是隻有一條線

@property (weak, nonatomic) IBOutlet UILabel *statusText; 

現在,當你聲明一個屬性,除非你合成它這樣你沒有得到一個名爲像變量:

@implementation Class 

@synthesize propertyName; 

@end 

這就是爲什麼statusText不存在的原因。

你有三個選擇:

  1. 合成statusText一樣,這樣你就可以使用該伊娃。
  2. 而不是直接訪問var,訪問像這樣的屬性self.statusText
  3. XCode正在創建它自己的變量,它可能被命名爲_statusText,像訪問變量那樣直接訪問變量。

您也可以使用2和3

+0

@emilioPalaez,你的一個救星,非常感謝你..還有一件事,你能推薦一本好書來學習,那真是太棒了.. :) – DanKiiing

+0

對不起,我不知道任何好書。我在大學學習了基本的C語言和Java編程,並且利用這些知識,我學習了一些Apple的文檔和示例,並開始了。 – EmilioPelaez

+0

感謝您的幫助@emiliopelaez – DanKiiing

0

組合我使用的是同一本書,並有這個問題了。我已經學會在.m文件中使用下劃線

- (IBAction)buttonPressed:(UIButton *)sender { 
    NSString *title = [sender titleForState:UIControlStateNormal]; 
    **_statusText**.text = [NSString stringWithFormat:@"%@ button pressed.", title]; 

}