Label display not instant with iPhone app

前端 未结 1 1668
刺人心
刺人心 2020-12-20 09:09

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,

相关标签:
1条回答
  • 2020-12-20 09:26

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

    0 讨论(0)
提交回复
热议问题