views:

85

answers:

1

Hello all,

Could somebody help me a little bit with my issue below?

When I call the myFunction, images which I want to set to buttons appear after 2 sec simultaneously, not one by one with delay of 0.5 sec.

More info:

  • generatedNumbers is array with four elements of NSNumber (4,1,3,2)
  • buttons are set in UIView via IB and are tagged (1,2,3,4)

-(IBAction) myFunction:(id) sender {
int i, value;
for (i = 0; i<[generatedNumbers count]; i++) {
    value = [[generatedNumbers objectAtIndex:i] intValue];
    UIButton *button = (UIButton *)[self.view viewWithTag:i+1];
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",value]];
    [button setImage:img forState:UIControlStateNormal];
    [img release];
    usleep(500000);
}   
}
+3  A: 

What's probably happening is that this method is running on the main thread. Since it waits to finish when you call usleep(), the run loop never gets to the point where it's going to update the UI. Try creating an NSTimer and firing off a separate method every .5 seconds; that should give the main thread enough time to update.

Jeff Kelley
Could you explain to me how to integrate NSTimer to my functionality?
Ivan
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
Jeff Kelley