Using a Progress Heads Up Display in your iPhone Program
Apple provides a simple and beautiful way to handle operations that may take a bit of time. Use the UIKit class UIProgressHUD to create a simple window with a rotating progress indicator and a short message. Programatically, all you have to do is allocate an object, initialize it with your main window, and tell it to go.
You can update the displayed text as desired. Send a new "setText:" to the HUD object at any time. When you are finished with your task, send it a show:NO message and the iPhone takes care of all the cleanup.
If you have a message that is longer than just a word or two, use the SetFontSize: method to adjust accordingly. I urge you to keep your messages short and to the point.
Full source follows after the jump.
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UIApplication.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView.h>
#import <UIKit/UIProgressHUD.h>
@interface SampleApp : UIApplication {
}
@end
@implementation SampleApp
- (void) killHUD: (id) aHUD
{
[aHUD show:NO];
}
- (void) applicationDidFinishLaunching: (id) unused
{
// Load the Default.png image
UIImage *img = [UIImage applicationImageNamed:@"Default.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
// Set up the window, order it front and make it key
struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
rect.origin.x = rect.origin.y = 0.0f;
UIWindow *window = [[UIWindow alloc] initWithContentRect: rect];
[window orderFront: self];
[window makeKey: self];
[window setContentView: imgView];
// Create the Heads Up Display
id HUD = [[UIProgressHUD alloc] initWithWindow:window];
[HUD setText:@"Downloading now..."];
[HUD show:YES];
// Kill the HUD after a delay
[self performSelector:@selector(killHUD:)
withObject:HUD afterDelay:2.0];
}
@end
Categories
MacComments (0)
Read More Entries by Erica Sadun.

Leave a comment