Filip Ekberg
Filip Ekberg

Reputation: 36327

Creating a custom loading screen on iPhone

I'd like to create a View/Window that performs some inital loading in my application. I tried something like this:

StartWindow *start = [[StartWindow alloc] initWithNibName:@"Start" bundle:nil];

self.startWindow = start;

[start release];

[window addSubview:startWindow.view];

And in the viewDidLoad event inside StartWindow for the time being I just have [NSThread sleepForTimeInterval:3.0]; to simulate my loading.

The problem is that my view doesn't display until after the thread finished sleeping. why?

Edit

The above code is inside didFinishLaunchingWithOptions.

Upvotes: 0

Views: 1077

Answers (2)

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

Because the framework is waiting for you to finish initialising the view in viewDidLoad. If you want loading to happen in the background you have to use some kind of background processing facility, like a separate thread or NSOperationQueue.

BTW, sleepForTimInterval doesn't actually run in a separate thread. It makes the calling thread sleep (in this case, the main UI thread).

Upvotes: 1

JustSid
JustSid

Reputation: 25318

The problem is that you block the main thread and thus the OS can't refresh the window and display your new view. You could try to perform the loading in a second thread, or, if you need to call a lot of non threadsafe functions, you could start the loading after a short amount of time via an NSTimer so that the OS has time to refresh the window.

Another way is to perform the loading in viewDidAppear: which gets invoked when the view is displayed while viewDidLoad gets invoked when the view got loaded from the nib file.

Upvotes: 1

Related Questions