【问题标题】:Change text in UILabel with time duration使用持续时间更改 UILabel 中的文本
【发布时间】:2010-11-04 20:27:44
【问题描述】:
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]; 

谁能给我一个例子,告诉我如何操作 iphone 的UILabel,每 1 分钟更改 100 个不同的单词?显然,您不必输入 100 个不同的单词,但不断添加单词的代码过程是什么,而不是每 1 分钟更改一次。感谢您的帮助。

【问题讨论】:

    标签: iphone xcode ios-simulator


    【解决方案1】:

    我不太清楚您所说的“不断添加单词”是什么意思。您可能想要的是一个 NSMutableArray 来存储单词。然后使用 NSTimer 每分钟触发一个方法以将标签设置为新单词。

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

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

    【讨论】:

    • 对不起,表述不清楚,我想问我如何在代码中输入更多的单词。我如何格式化一个单词库,让它每 1 分钟变成“其他东西”
    【解决方案2】:

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

    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] 来获取您添加到可变数组中的最后一个字符串。

    【讨论】:

    • 所以我假设我一直将我想说的下一个单词放入“其他内容”中 我是否一直在复制每个单词的 changeLabel 代码?我如何将所有单词编码到其中。
    • 使用我给出的示例的 [wordArray lastObject] 版本,并在代码中的其他地方执行 [wordArray addObject:@"Next word"]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2021-01-18
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多