Search code examples
objective-cioscocoa-touchmbprogresshud

Show loading indicator before loading a view


I have a view, this view takes some time in loading, so after I click in a "Show me the view" button, it takes like 2 seconds to load.

I have no problem with this time, but I would like to add a "Loading" message from the moment the button is pressed until the new view is shown.

I'm using MBProgressHUD, but I'm using a different thread to load the view, so this is not a good idea.


Code I have so far

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    [self presentModalViewController:testViewController animated:YES];

    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

I just want to show the "loading" until the next view actually loads. Using this approach I have tons of problems in the next view for not executing viewDidLoad method in the main thread.

Any idea how I can fix this?


Solution

  • What you will want to do is load the other view on the main thread and once the view is loaded show the MBProgressHUD. Then perform the operations that are taking a while in the background using the dispatch_async approach.