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.
- Use an alertview
- 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.
- Download it from given link
- Import Both file to your project ( copy not link )
#import "MBProgressHUD.h"
- import this where you want to use progress view.MBProgressHUD *mbProcess;
- declare a variable in *.h file.- 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];
- When your processing is done place this line.
[mbProcess hide:YES];
- 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
精彩评论