2012-12-05 40 views
0

我有一個application,您在那裏按下button,當您按下按鈕時,標籤隨機詞變化。但是當我按下按鈕我的label消失。從數組中獲取值並將其放在文本標籤中

我該怎麼辦?

這是代碼:

if (sender == self.button) { 

     NSString*path = [[NSBundle mainBundle]pathForResource:@"wordss" ofType:@"plist"]; 
     words = [[NSMutableArray alloc]initWithContentsOfFile:path]; 
     [self.randomLabel setText:[self.words objectAtIndex:arc4random_uniform([self.words count])]]; 
} 

回答

0

標籤不消失的問題是要設置爲空字符串作爲選取的隨機值返回空值。

NSString*path = [[NSBundle mainBundle]pathForResource:@"wordss" ofType:@"plist"]; 
words = [[NSMutableArray alloc]initWithContentsOfFile:path]; 
NSLog("%@",words); 
NSString *string = [NSString stringWithFormat:[self.words objectAtIndex:((arc4random() % [self.words count]) + 0)]] 
NSLog("%@",string); 
[self.randomLabel setText:string]; 

檢查的NSLog語句的不是空

0

你檢查的輸出:

arc4random_uniform([self.words計數])

而且,還因爲標籤可以採取的NSString ,所以你需要將int轉換回NSString。所以,做這樣的:

NSString *myGeneratedRandNumber = [NSString stringWithFormat:@"%d", arc4random_uniform([self.words count])]; 

然後,這個字符串設爲您的標籤,這樣的:

[myLabel setText:myGeneratedRandNumber]; 

另外請注意,此功能的工作:

下將生成一個號碼介於0到73之間。

arc4random_uniform(74); 
+0

Tnx它的工作幾乎只有標籤顯示數字而不是字符串。這怎麼可能? – T1992

+0

對不起,但我沒有得到你。請澄清你想知道什麼?請告訴我。 :) –

+0

在words.plist中每個刺都有一個類似於項目o,項目1等的按鍵。當我按下按鈕時,標籤顯示的是密鑰而不是密鑰的值 – T1992

相關問題