#import <UIKit/UIKit.h>
@interface CommonLoadingViewController : NSObject
{
UIActivityIndicatorView *LoadingView;
UIView *backgroundView;
}
-(void)loadingView;
-(void)finishedLoading;
+ (CommonLoadingViewController*)sharedLoading;
@end
#import "CommonLoadingViewController.h" #import "GenericAppDelegate.h" #import "KDesign.h" @implementation CommonLoadingViewController static CommonLoadingViewController *sharedGizmoManager = nil; //___________________________________________________________________________ + (CommonLoadingViewController*)sharedLoading { if (sharedGizmoManager == nil) { sharedGizmoManager = [[super allocWithZone:NULL] init]; } return sharedGizmoManager; } //___________________________________________________________________________ + (id)allocWithZone:(NSZone *)zone { return [[self sharedLoading] retain]; } //___________________________________________________________________________ - (id)copyWithZone:(NSZone *)zone { return self; } //___________________________________________________________________________ - (id)retain { return self; } //___________________________________________________________________________ - (NSUInteger)retainCount { return NSUIntegerMax; //denotes an object that cannot be released } //___________________________________________________________________________ - (id)autorelease { return self; } //___________________________________________________________________________ -(void)loadingView{ backgroundView = [[UIView alloc] init]; backgroundView.frame = CGRectMake(0, 0,kWidth,kHeight); backgroundView.backgroundColor = [UIColor colorWithRed:(40/255.0f) green:(40/255.0f) blue:(40/255.0f) alpha:1.0f]; backgroundView.alpha = 0.4; [[getDelegate window] addSubview:backgroundView]; LoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [LoadingView setCenter:CGPointMake(kWidth/2, kHeight/2 - 40)]; LoadingView.hidesWhenStopped = YES; //LoadingView.color = [UIColor grayColor]; LoadingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; [backgroundView addSubview:LoadingView]; [viewController.view.window bringSubviewToFront:backgroundView]; [LoadingView startAnimating]; } //___________________________________________________________________________ -(void)finishedLoading{ [LoadingView stopAnimating]; [LoadingView removeFromSuperview]; [backgroundView removeFromSuperview]; } #pragma mark - #pragma mark Memory management //___________________________________________________________________________ - (void)dealloc { [backgroundView release]; [LoadingView release]; [super dealloc]; } @end
备注:
网上有很多单例的例子。不过大部分都是只有实现部分的。
这是我写的关于loading界面的一个单例。
如有不好请指正。
调用的时候:
#define getCommonLoading [(GenericAppDelegate *)[[UIApplication sharedApplication] delegate] commonLoading] commonLoading = [CommonLoadingViewControllersharedLoading];//重复调用放到appdelegate里面 [getCommonLoading loadingView]; [getCommonLoading finishedLoading];