标签显示与iPhone应用程序不能即时 [英] Label display not instant with iPhone app

查看:151
本文介绍了标签显示与iPhone应用程序不能即时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为iPhone开发的应用程序。我的问题是如何与每半秒不同的文本显示一个新的标签。例如,它会显示蓝色,红色,绿色,橙色和紫色;一个接一个一个正确的。现在我这样做:

I am developing an application for the iPhone. The question I have is how to display a new label with a different text every .5 seconds. For example, it would display Blue, Red, Green, Orange and Purple; one right after one another. Right now I am doing this:

    results = aDictionary;
    NSArray *myKeys = [results allKeys];
    NSArray *sortedKeys = [myKey sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    int keyCount = [sortedKeys count];
    while (flag == NO) {


        NSTimeInterval timeMS = [startDate timeIntervalSinceNow] * -10000.0;            

        if (timeMS >= i) {
            ii++;
            i += 1000;
            NSLog(@"endDate = %f", timeMS);
            int randomNumber = rand() % keyCount  + 1;
            lblResult.text = [results valueForKey:[sortedKeys objectAtIndex:(randomNumber - 1)]];
            result = [results valueForKey:[sortedKeys objectAtIndex:(randomNumber - 1)]];
            lblResult.text = result;

       }
        if (ii > 25) {
            flag = YES;
        }
    }
    lblResult.text = [results valueForKey:[sortedKeys objectAtIndex:(sortedKeys.count - 1)]];

这个函数被调用的函数viewDidAppear,目前没有显示新的标签。它仅显示一个在末尾。难道我做错了什么?什么是解决这个的最好方法?

this function is called at the viewDidAppear Function and currently isn't displaying the new labels. It only displays the one at the end. Am I doing anything wrong? What would be the best method to approach this?

推荐答案

的问题是,你不给运行的循环运行的机会(因此,绘图发生)。你要使用的NSTimer 的火灾定期,并设置下一个文本(你可以在一个实例变量您当前所在记得了)。

The problem is that you're not giving the run loop a chance to run (and therefore, drawing to happen). You'll want to use an NSTimer that fires periodically and sets the next text (you could remember in an instance variable where you currently are).

或者使用这样的(假设项目是的NSArray 牵着你的字符串):

Or use something like this (assuming that items is an NSArray holding your strings):

- (void)updateText:(NSNumber *)num
{
    NSUInteger index = [num unsignedInteger];
    [label setText:[items objectAtIndex:index]];
    index++;

    // to loop, add
    // if (index == [items count]) { index = 0; }

    if (index < [items count]) {
        [self performSelector:@selector(updateText:) withObject:[NSNumber numberWithUnsignedInteger:index] afterDelay:0.5];
    }
}

在开始的时候(例如,在 viewDidAppear:),你可以再调用

[self updateText:[NSNumber numberWithUnsignedInteger:0]];

触发初始更新。

你当然需要确保当你的视野中消失的执行不会继续,你可以通过取消performSelector做到这一点,或者如果你使用一个计时器,通过简单的无效,或使用一个布尔值,或...

You'd of course need to ensure that the performs are not continuing when your view disappears, you could do this by canceling the performSelector, or if you're using a timer, by simply invalidating it, or using a boolean, or ...

如果你想获得真正看中的,使用GCD:)

And if you want to get really fancy, use GCD :)

这篇关于标签显示与iPhone应用程序不能即时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆