Tuesday 26 August 2014

Display LoadingView for any Device from AppDelegate on any UIViewController

Here this bellow code is used to display Custom LoadingView on any View whenever you want.
Whenever you call any url or web-service at that time activity indicator or loading view required for display that system in process so user wait for the response.

Here using my bellow code and methods you can easily Apply this code in your projects and easily you can use it in any class.

Here another method is used to get instance of AppDelegate and you can call It's method from any class.

First in AppDelegate.h file just create this bellow objects and methods.

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
    
    UIView *activityView;
    UIView *loadingView;
    UILabel *lblLoad;
    
}

-(void) showLoadingView;
-(void) hideLoadingView;
+(AppDelegate *)sharedInstance;
@end

and in AppDelegate.m class just paste these bellow code...

#pragma  Loading View

-(void) showLoadingView {
    if (loadingView == nil) {
        loadingView = [[UIView alloc] initWithFrame:self.window.frame];
        UIImageView *imgBack = [[UIImageView alloc]initWithFrame:loadingView.frame];
        [loadingView addSubview:imgBack];
        imgBack.opaque = NO;
        imgBack.backgroundColor = [UIColor darkGrayColor];
        imgBack.alpha = 0.5;
        UIView *subloadview=[[UIView alloc] initWithFrame:CGRectMake(84.0, 190.0,150.0 ,50.0)];
        subloadview.backgroundColor=[UIColor blackColor];
        subloadview.opaque=NO;
        subloadview.alpha=0.6;
        subloadview.layer.masksToBounds = YES;
        subloadview.layer.cornerRadius = 6.0;
        lblLoad=[[UILabel alloc]initWithFrame:CGRectMake(50.0, 7.0,80.0, 33.0)];
        lblLoad.text=@"LoadingView";
        lblLoad.backgroundColor=[UIColor clearColor];
        lblLoad.textColor=[UIColor whiteColor];
        [subloadview addSubview:lblLoad];
        UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10.0, 11.0, 25.0, 25.0)];
        [spinningWheel startAnimating];
        spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        [subloadview addSubview:spinningWheel];
        subloadview.center = loadingView.center;

        [loadingView addSubview:subloadview];
    }
    [self.window addSubview:loadingView];
}

-(void) hideLoadingView {
    if (loadingView) {
        [loadingView removeFromSuperview];
        loadingView = nil;
    }
    
}

+(AppDelegate *)sharedInstance
{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}



whenever you want to display loadingview then just use bellow code...

        [[AppDelegate sharedInstance]showLoadingView];

and for this you just need to import AppDelegate.h file in you class where you use above code of showLoadingView like bellow..

#import "AppDelegate.h"