karse23
karse23

Reputation: 4115

(Iphone Sdk) How to display animated activity Indicator while a view controller is loading?

I have a so heavy view that takes two seconds or more 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.

Upvotes: 0

Views: 7716

Answers (5)

Splendid
Splendid

Reputation: 1061

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

Upvotes: 1

Yorxxx
Yorxxx

Reputation: 526

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

Upvotes: 3

Olaf
Olaf

Reputation: 3042

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.

Upvotes: 1

acorc
acorc

Reputation: 360

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

Upvotes: 1

Matthias Bauch
Matthias Bauch

Reputation: 90117

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.

Upvotes: 1

Related Questions