使用持续时间更改UILabel中的文本

时间:2010-11-04 20:27:45

标签: iphone xcode ios-simulator

UILabel *scoreLabel = [ [UILabel alloc ] initWithFrame:CGRectMake((self.bounds.size.width / 2), 0.0, 150.0, 43.0) ]; 

scoreLabel.textAlignment =  UITextAlignmentCenter; 

scoreLabel.textColor = [UIColor whiteColor]; 

scoreLabel.backgroundColor = [UIColor blackColor]; 

scoreLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(36.0)]; 

[self addSubview:scoreLabel]; 

scoreLabel.text = [NSString stringWithFormat: @"%d", score]; 

有人能告诉我一个例子,我如何操纵i UILabel,每1分钟改变100个不同的单词?显然你不必输入100个不同的单词,但是代码的过程是什么,以便继续添加单词,并且每1分钟更改一次。谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

我不太确定“继续添加单词”是什么意思。您可能需要的是一个NSMutableArray来存储单词。然后使用NSTimer每分钟触发一个方法将标签设置为一个新单词。

如何使用NSTimer:How do I use NSTimer?

如何使用NSMutableArray:http://www.cocoadev.com/index.pl?NSMutableArray

答案 1 :(得分:0)

设置计时器,然后在完成后使其无效。

myTimer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(changeLabel) userInfo:nil repeats:YES];

-(void)changeLabel {
    scoreLabel.text = [wordArray objectAtIndex:cntr];
    cntr++;
    if(cntr==100) [myTimer invalidate];
}

此示例假设您有100个单词的NSMutableArrayNSArray。如果你有越来越多的单词,你可以使用[wordArray lastObject]来获取你添加到可变数组的最后一个字符串。