Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

iphone - How do I update the UI in the middle of this thread?

Below is a block of code that runs in a separate thread from my app's main thread. How do I get the UI to update after each button gets its thumbnail? Right now it doesn't update until the whole method finishes. The buttons are already added to a UIScrollView.

(LotsGridButton is just a UIButton with some extra properties.)

- (void)fetchThumbnails {
    CCServer* server = [[CCServer alloc] init];
    for (int i=0; i<[buttons count]; i++) {
        LotsGridButton* button = [buttons objectAtIndex:i];     
        if (button.lot.thumbnail) continue;
        // load the thumbnail image from the server
        button.lot.thumbnail = [server imageWithPath:button.lot.thumbnailURL];
        [button setImage:button.lot.thumbnail forState:UIControlStateNormal];
    }
    [server release];
}
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

In place of setImage:forState:, take a look at the performSelectorOnMainThread: method, e.g.:

[myButton performSelectorOnMainThread:@selector(setThumbnail:) withObject:[server imageWithPath:myButton.lot.thumbnailURL] waitUntilDone:NO];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...