Andrew Igumenoff Andrew Igumenoff - 1 year ago 88
iOS Question

UIView is shown after IBAction method returns

I've got the following code fragment (for illustration purpose):

- (IBAction)buttonPress:(id)sender {
UIView *overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; =;
[overlayView addSubview:activityIndicator];
[self.navigationController.view addSubview:overlayView];
//[self.navigationController.view bringSubviewToFront:overlayView];
[activityIndicator startAnimating];


//[overlayView removeFromSuperview];

I wonder why the overlay view is only displayed after the method returns...

Answer Source

I guess it's because sleep(10) blocks the main thread which is responsible for performing UI related activities, such as drawing on screen.

Remove the sleep(10) line and should work fine.

Here's on link to SO in case you're interested how to use delays in coca-touch:
Adding delay between execution of two following lines