I have a so heavy view that takes two seconds or mor开发者_如何转开发e seconds to load. I would like to show activity indicator while be carrying all things at the viewDidLoad and then hide it. Can someone guide me to do this? Thanks in advance.
Try MBProgressHUD by Matej Bukovinski.
https://github.com/matej/MBProgressHUD
Fetching data in background
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD showWhileExecuting:@selector(getStartupData:)
onTarget:self
withObject:nil
animated:YES]
If you want to get data in main thread, just show the HUD and then dismiss it when notified.
HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
[self.tableView addSubview:HUD];
HUD.labelText = @"Loading";
HUD.tag = 998;
[HUD show:YES];
[HUD release];
I don't know for sure but I doubt that it's possible to run viewDidLoad on a background thread. If this is true you have no way to animate an activity indicator when running viewDidLoad.
So If I were you I would figure out what takes viewDidLoad so long, and put this into a method that can run in the background.
Then add a activity indicator view in your viewWillAppear method and remove it when your background task is complete.
Depending on the stuff you do in viewDidLoad this might require a lot of refactoring and adding of delegate methods.
I guess you are downloading something that should be displayed.
Try moving the stuff that's taking so long into viewDidAppear. Then you can create a new view with the activity indicator and have it visible (easiest in interface builder) and once all your stuff is done just call:
[loadingView setHidden:YES];
good luck
Put [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
in the view from where you call the slow loading viewController, or into appDidFinishLoading if the slow starter is the initial view.
Additionally, consider moving the activity that takes a long time from the main thread into a background thread, keeping the GUI always responsive.
you can do this very simply in viewDidLoad
UIAactivityIndicatorView * date = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 150, 30, 30)];
[date setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addsubview:date];
[date startAnimating];
After loading the page view, there u stop animate the Acitivity indicator by [date stopAnimating];
this will work
精彩评论