2014-06-17 36 views
0

標籤文本在myProj.h文件我宣佈:集等於數組值

@property (strong, nonatomic) NSMutableArray *wordsArray; 
在我使用以下方法來在viewDidLoad中添加一些值.m文件

[_wordsArray addObject:[NSString stringWithFormat:@"Now is the time for all good men to come to the aid of their party."]]; 
[_wordsArray addObject:[NSString stringWithFormat:@"Four score and seven years ago our forefathers..."]]; 
[_wordsArray addObject:[NSString stringWithFormat:@"A coward dies many deaths; a brave man but one."]]; 

我也是在這裏設置一個計數器(在.h文件中定義爲int):

__cntr = 0; 

在(IBAction爲)輕觸式:(ID)發送我想變化率T他將(* somelabel)文本標記爲數組[0]中的下一個值(然後將_cntr重複爲1並獲取該值,等等)。所以我 有:

_somelabel.text = [NSString stringWithFormat:@"%@",[_wordsArray objectAtIndex:__cntr]]; 

構建但是當我運行,並按下按鈕上的標籤文字變成(空)。

所以就是向數組中添加值或將其拉出的問題。謝謝

+1

爲什麼你有'wordsArray'公共財產?其他類是否設置數組或從中獲取值?如果不是,它不應該是公共財產。 – rmaddy

回答

5

我的猜測是你還沒有初始化你的可變數組。在viewDidLoad中添加:

_wordsArray = [[NSMutableArray alloc] init]; 

在向其添加任何對象之前。