2012-12-05 42 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

相关问题