Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to show a loading screen in an iPhone app?

I'm building what is essentially a web app. Every piece of data has to be fetched from a web API. So, every UITableView that I show takes some amount of time to fill with data and I'm struggling to find a good way to show the user a loading screen.

Right now I'm popping up an action sheet, but that just seems a bit wrong. Ideally, I'd popup up a blank view over the tableview with "Loading..." on it, then fade it away when the data comes in, but I can't think of a way to do that in 8 places in my app without massive code duplication.

like image 984
Phil Kulak Avatar asked Dec 02 '22 05:12

Phil Kulak


1 Answers

there are two options for you according to me.

  1. Use an alertview
  2. Use MBProgressHUD.

  • For alertView You have to place a UIAlertView variable in .h file. Then place following code - when you are requesting/loading data.

    av=[[UIAlertView alloc] initWithTitle:@"Loading Data" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    ActInd=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [ActInd startAnimating];
    [ActInd setFrame:CGRectMake(125, 60, 37, 37)];
    [av addSubview:ActInd];
    [av show];

Now, place following statement when you have finished your processing.

[av dismissWithClickedButtonIndex:0 animated:YES];
[av release]; av=nil;

  • Another way is using MBProgressHUD
  • It's very useful & ready made for you - you just have to follow these steps.

  1. Download it from given link
  2. Import Both file to your project ( copy not link )
  3. #import "MBProgressHUD.h" - import this where you want to use progress view.
  4. MBProgressHUD *mbProcess; - declare a variable in *.h file.
  5. Place following code when you are processing ( in *.m file )

mbProcess=[[MBProgressHUD alloc] initWithView:self.view];
mbProcess.labelText=@"Loading Data";
[self.view addSubview:mbProcess];
[mbProcess setDelegate:self];
[mbProcess show:YES];

  1. When your processing is done place this line. [mbProcess hide:YES];
  2. Don't forget to add delegate method of MBProgressHUD in you *.m file. like this

#pragma mark -
#pragma mark MBProgressHUDDelegate methods

- (void)hudWasHidden {
    // Remove HUD from screen when the HUD was hidded
    [mbProcess removeFromSuperview];
    [mbProcess release];
}
like image 101
Sagar Kothari Avatar answered Dec 06 '22 00:12

Sagar Kothari