Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
948 views
in Technique[技术] by (71.8m points)

objective c - Label display not instant with iPhone app

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)]];

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

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];
    }
}

At the beginning (e.g. in viewDidAppear:), you could then call

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

to trigger the initial update.

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 ...

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...