How do I create a reusable loading screen?

How to create a loading screen that can be reused at any given time. I know the Default.png, but I need the flexibility to hook up the loading screen at any point in the app's lifecycle.

This is what I have so far.

//inside a method that gets called by a UIButton

LoadingViewController* loadController = [[LoadingViewController alloc] initWithNibName:@"Loading" bundle:nil vertical:NO];
[self.view addSubview: loadController.view];

//some method call that takes a few seconds to execute
[self doSomething];

//This loads some other view, my final view
[self.view addSubview: someOtherView]

      

but it seems that the download view is never shown. Instead, the previous view remains there until "someOtherView" is added. I put the trace logs and the code seems to execute, I even replaced [self doSomething] with sleep (2), but the intermediate boot view is never shown.

If I remove [self.view addSubview: someOtherView]; then a few seconds later ... (after doSomething finishes executing) the boot view is displayed as there is no view to be pushed on top of it, but this is obviously not the functionality I want.

Can you explain this behavior? Is there something about the render loop that I'm misunderstanding because it doesn't seem like the view (at least on the screen) is instantly refreshed even though I'm calling [self.view addSubview: loadController.view];

Do I need to create a separate thread?

+1


a source to share


2 answers


In general, in order for the UI changes to be made visible to the user, control must fall back to the main runLoop. You only fall back to runLoop after you lower the load image and replace it with a different view. One strategy to combat this is to move the code that is loading onto another thread. You can use NSOperation and NSOperationQueue for this.

An even simpler approach is to use the performSelectorInBackground: withObject function to perform the processing. Upon completion of processing, the user interface can be refreshed again to display the data. It's important to remember that UI updates must be done on the main thread. Use performSelectorOnMainThread: withObject: waitUntilDone: execute this from the boot thread.



It sounds like a lot of complications, but it's really as easy as breaking one method into three separate methods like this:

  • Display loading view and start background process is the action method of the button.
  • Performs background loading - Called from a button action function using the performSelectorInBackground: withObject function.
  • Remove the load view and update the display with data called from the background thread using performSelectorOnMainThread: withObject: waitUntilDone.
+4


a source


I created a UIView subclass where I initialized how my boot view should work and look like. (My gaze appeared and slid from below with a nice animation).

Then I added code that handled whether the upload view should be visible or not in the UIViewController subclass.

Then I allow all of my view controllers to be a subclass of my new viewcontrollerclass, which allowed me to do:



[self showloadingMessage:@"loading..."];

      

in all my view controllers ...

+1


a source







All Articles