Question

I have two view controllers - I will simply call them A and B to help you understand with ease.

A view controller has a button linked to B by storyboard (not manually coded). And, B view controller retrieves bunch of json data from DB server in viewDidLoad method. Because retrieving json data takes some time, I need to add a activity indicator to let users know it's doing something - not frozen. However, I'm not quite sure where I should add the activity indicator view.

If you were in my shoes, where would you add it?

Was it helpful?

Solution 2

If you have a blocking call in viewDidLoad that will cause the UI to freeze on A before moving to B so I would suggest adding the Activity Indicator on A. Although you may notice that because of the blocking call to the DB, your UI changes immediately before the blocking call also get frozen and you won't see any UI change.

The way I usually handle this is by spawning a new thread for the blocking call thus freeing up the main thread for UI and then adding an Activity Indicator where necessary (on A or B depends on design but I would recommend showing it on B in this case from a usability POV).

Just to avoid the hassle of managing all this on my own, I usually end up using something like MBProgressHUD or a similar library which are much easier to manage.

OTHER TIPS

Use the normal interface builder to position it where you would like it to be seen, and set the hidesWhenStopped variable to YES so that it isn't visible after you've finished getting your data. Then just tell it to stop in your JSON callback.

In terms of design, the 'done thing' is to put it in the middle of whatever is being loaded. For an image, this would be the middle of where the image will be shown. For a table you might go for a large activity indicator in the middle of the screen, or on an empty 'placeholder' cell. Think about where the user's eye will travel to try and see the data they want, and that's where they should see the indicator.

I would use an alert view with a message in the title then you add a sub view to the alert which is the activity indicator.

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Doing something\nPlease Wait..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[alertView show];
indicator.center = CGPointMake(alertView.bounds.size.width / 2, alertView.bounds.size.height - 50);
[alertView addSubview:indicator];
[indicator startAnimating];

later on in code dismiss...

[alert dismissWithClickedButtonIndex:0 animated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top