//
// UIAlertView+ProgressHUD.h
// WPUIKit
//
// Created by Peng Leon on 12/11/26.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIAlertView (ProgressHUD)
@property(retain, nonatomic) UIActivityIndicatorView *actView;
- (id)initProgressHUDWithTitle:(NSString *)title;
- (void)dismiss;
@end
//
// UIAlertView+ProgressHUD.m
// WPUIKit
//
// Created by Peng Leon on 12/11/26.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "UIAlertView+ProgressHUD.h"
#import <objc/runtime.h>
NSString * const MyActivityIndicatorViewKey = @"kMyActivityIndicatorViewKey";
@implementation UIAlertView (ProgressHUD)
@dynamic actView;
- (id)initProgressHUDWithTitle:(NSString *)title
{
self = [self initWithTitle:title
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
if (self) {
[self performSelector:@selector(addActivityIndicatorViewInAlertView)];
}
return self;
}
- (void)setActView:(UIActivityIndicatorView *)actView
{
objc_setAssociatedObject(self, &MyActivityIndicatorViewKey, actView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIActivityIndicatorView *)actView
{
UIActivityIndicatorView *myView = objc_getAssociatedObject(self, &MyActivityIndicatorViewKey);
if (myView == nil) {
myView = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
[myView setFrame:CGRectMake(125., 50., 30., 30.)];
objc_setAssociatedObject(self, &MyActivityIndicatorViewKey, myView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return myView;
}
- (void)addActivityIndicatorViewInAlertView
{
CGRect viewFrame = [self bounds];
NSLog(@"view frame -> %@", NSStringFromCGRect(viewFrame));
[self addSubview:self.actView];
[self.actView startAnimating];
}
- (void)dismiss
{
[self.actView stopAnimating];
self.actView = nil;
[self dismissWithClickedButtonIndex:-1 animated:YES];
}
@end
- (IBAction)doProgressHUD:(id)sender
{
_progressHUD = [[UIAlertView alloc] initProgressHUDWithTitle:@"Load Data..."];
[_progressHUD show];
[self performSelector:@selector(dismissProgressHUD) withObject:nil afterDelay:3];
}
- (void)dismissProgressHUD
{
NSLog(@"call dismissProgressHUD function.");
[_progressHUD dismiss];
}