Progress HUDs: When It's Important Enough to Block Stuff

One other progress object exists, but is not an indicator itself. The UIProgressHUD class displays a UIProgressIndicator object and any accompanying text in a semi-transparent window. When you want to get the message across to the user that she really shouldn't be doing anything in a certain window until the operation is completed, the UIProgressHUD class is the class for you. The window is superimposed over the entire view window, dimming out and effectively blocking access to any other components on the view. This can be seen on the iPhone when certain carrier-level features are changed, and when sending SMS messages by blocking access to the keyboard.

To create a UIProgressHUD object, initialize it along with the display region of the parent object:

UIProgressHUD *hud = [ [ UIProgressHUD alloc ] initWithFrame: viewRect ];

The text of the HUD is set using the object's setText method.

[ hud setText: @"Please Wait. I'm doing something REALLY important." ];

Then, attach the HUD to a large view or window.

[ mainView addSubview: hud ];

Even though the object encapsulates a UIProgressIndicator, no startAnimation or stopAnimation methods are directly accessible. The HUD's indicator is activated and deactivated by invoking a show method.

[ hud show: YES ];

When told to show, the HUD will gray out the parent view. It will then activate the spinning indicator and display the text it was configured with. When your application has finished ...

Get iPhone Open Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.