开发者

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

I'm buildi开发者_运维知识库ng 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.


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];
}


If you want a nice looking spinner, look at http://github.com/matej/MBProgressHUD

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜