Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, August 2, 2016

Use of MBProgressHUD Globally + make it singleton

Use of MBProgressHUD Globally + make it singleton


In my Project, each of the user interaction events make a network call (Which is TCP, not HTTP). I need Activity Indicator to be global to show from a random UIViewController and hide from NetworkActivityManager Class (a custom class to handle network activities, Which is not a subclass of UIViewController or UIView).

After searching the web I found out that MBProgressHUD is used for the same purpose, but I wasn't able to find out an example on how would I use it globally. (By saying global I mean a singleton object of MBProgressHUD and class methods to SHOW and HIDE it.)

Following is what I have tried yet, but, failed: In AppDelegate.h:

@property (nonatomic, retain) MBProgressHUD *hud;

In AppDelegate.m:

@synthesize hud;

In some random UIViewController object:

appDelegate.hud = [MBProgressHUD showHUDAddedTo:appDelegate.navigationController.topViewController.view animated:YES];  appDelegate.hud.labelText = @"This will take some time.";  

And while hiding it, from NetworkActivityManager Class:

[MBProgressHUD hideHUDForView:appDelegate.navigationController.topViewController.view animated:YES];  

This makes the project to crash after some time (due to memory issues.) I am using ARC in my project and also, I am using the ARC version of MBProgressHUD.

Am I missing something?

Important Question:

Can I make MBProgressHUD work like UIAlertView? (Saying that I mean implementation of MBProgressHUD independent of UIView -- sa it uses showHUDAddedTo: to present itself) ???

Please Note: In the above code of hiding MBProgressHUD, View may be changed from what it was when showing MBProgressHUD.

Any Help greatly appreciated.

Answer by Maulik for Use of MBProgressHUD Globally + make it singleton


I've used it as below..Hope it helps you..

in appDelegate.m

-(void)showIndicator:(NSString *)withTitleString currentView:(UIView *)currentView  {   if (!isIndicatorStarted) {        // The hud will dispable all input on the view      self.progressHUD = [[[MBProgressHUD alloc] initWithView:currentView] autorelease];       // Add HUD to screen       [currentView addSubview:self.progressHUD];       self.progressHUD.labelText = withTitleString;      [window setUserInteractionEnabled:FALSE];      [self.progressHUD show:YES];        isIndicatorStarted = TRUE;  }      }     -(void)hideIndicator   {         [self.progressHUD show:NO];       [self.progressHUD removeFromSuperview];       self.progressHUD = nil;      [window setUserInteractionEnabled:TRUE];      isIndicatorStarted = FALSE;  }  

From Random Views:-

[appDel showIndicator:@"Loading.." currentView:presentView.view];

Answer by Matej Bukovinski for Use of MBProgressHUD Globally + make it singleton


You could add this to a class of your liking:

+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {      UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];      MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];      hud.labelText = title;      return hud;  }    + (void)dismissGlobalHUD {      UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];      [MBProgressHUD hideHUDForView:window animated:YES];  }  

This can be than called on any class. You don't need to keep a strong reference to the HUD when using those class convenience methods.

Depending on your specific situation you'll probably also want to handle cases where a new hud is requested before the other one is hidden. You could eater hide the previous hud when a new comes in or come up with some sort of queueing, etc.

Hiding the previous HUD instance before showing a new one is pretty straightforward.

+ (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {      UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];      [MBProgressHUD hideAllHUDsForView:window animated:YES];      MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];      hud.labelText = title;      return hud;  }  

Answer by Zoltan Varadi for Use of MBProgressHUD Globally + make it singleton


This answer is what I've been using for 5-6 Apps now because it works perfectly inside blocks too. However I found a problem with it. I can make it shown, but can't make it disappear if a UIAlertView is also present. If you look at the implementation you can see why. Simply change it to this:

static UIWindow *window;    + (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {        window = [[[UIApplication sharedApplication] windows] lastObject];      MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];      hud.labelText = title;        return hud;  }    + (void)dismissGlobalHUD {        [MBProgressHUD hideHUDForView:window animated:YES];  }  

This will make sure you're removing the HUD from the same windows as it was shown on.

Answer by Michael Shang for Use of MBProgressHUD Globally + make it singleton


I found @Matej Bukovinski 's answer very helpful, since I just started using Swift and my purpose using his methods was to set a global font for the MBProgressHUD, I have converted the code to swift and am willing to share the code below:

class func showGlobalProgressHUDWithTitle(title: String) -> MBProgressHUD{      let window:UIWindow = UIApplication.sharedApplication().windows.last as! UIWindow      let hud = MBProgressHUD.showHUDAddedTo(window, animated: true)      hud.labelText = title      hud.labelFont = UIFont(name: FONT_NAME, size: 15.0)      return hud  }    class func dismissGlobalHUD() -> Void{      let window:UIWindow = UIApplication.sharedApplication().windows.last as! UIWindow      MBProgressHUD.hideAllHUDsForView(window, animated: true)  }  

The above code is put into a global file where I keep all my global helpers and constants.

Answer by shweta sharma for Use of MBProgressHUD Globally + make it singleton


Add these two methods to show or hide loader in your singleton class

- (void)startLoaderWithText:(NSString *)title View:(UIView *)view{      progressHud = [MBProgressHUD showHUDAddedTo:view animated:YES];      progressHud.labelText = title;      progressHud.activityIndicatorColor = [UIColor grayColor];      progressHud.color = [UIColor clearColor];      [progressHud show:YES];  }    - (void)stopLoader{      [progressHud hide:YES];  }  

Answer by iOS App Dev for Use of MBProgressHUD Globally + make it singleton


Note: Considering the views this Question is getting I decided to post the the way I did choose as a solution. This is NOT an answer to my question. (Hence, the accepted answer remains accepted)

At that time I ended up using SVProgressHUD as it was very simple to integrate and use.

All you need to do is just drag the SVProgressHUD/SVProgressHUD folder into your project. (You may choose to go for cocoapods OR carthage, as well)


In Objective-C:

[SVProgressHUD show]; // Show  [SVProgressHUD dismiss]; // Dismiss  

In Swift:

SVProgressHUD.show() // Show  SVProgressHUD.dismiss() // Dismiss  

Additionally, Show and hide HUD needs to be executed on main thread. (Specifically you would need this to hide the HUD in some closure in background)

e.g.:

dispatch_async(dispatch_get_main_queue(), ^{      [SVProgressHUD dismiss]; // OR SHOW, whatever the need is.  });  

There are additional methods for displaying custom messages with HUD, showing success/failure for short duration and auto dismiss.

MBProgressHUD still remains a good choice for developers. It's just that I found SVProgressHUD to suit my needs.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.