MBProgressHUD indicator does not hide - ios

i am able to show HUD indicator in viewDidLoad successfully but not able hide it in webViewDidFinishLoad method when webview is completely loaded. Please help.
i am using below code::
in .h file
MBProgressHUD *HUD;
in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *query = [[NSString alloc] initWithFormat:#"http://localhost/index.php?uid=%#", [[UIDevice currentDevice] uniqueIdentifier]];
NSURL *url = [[NSURL alloc] initWithString:query];
NSString *response = [[NSString alloc] initWithContentsOfURL:url];
if(response)
{
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
else
{
//NSLog(#"err %#",response);
}
HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
HUD.delegate = self;
HUD.labelText = #"loading";
}
and in webViewDidFinishLoad
- (void)webViewDidFinishLoad:(UIWebView *)web
{
[HUD hide:TRUE]; //it does not work for me :(
}

i have fixed the error, i moved the code from viewDidLoad to webViewDidStartLoad and everything is working fine this time :)
- (void)webViewDidStartLoad:(UIWebView *)web
{
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = #"loading";
}
- (void)webViewDidFinishLoad:(UIWebView *)web
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}

try with this one
[HUD hide:YES];
if(HUD!=nil && [HUD retainCount]>0)
{
[HUD removeFromSuperview];
[HUD release];
HUD=nil;
}

You should not call MBProgressHUD from viewDidLoad, try calling it from viewDidAppear and everything should work well.

Try removing it using this class method:
+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated
.
- (void)webViewDidFinishLoad:(UIWebView *)web
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
If you use this method then you should think about rewriting your viewDidLoad this way:
- (void)viewDidLoad
{
[super viewDidLoad];
//...
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = #"loading";
}

Related

Update progress of MBProgressHUD

I have a scenario in which my app downloads a large chunk of data only for the first time and this happens in say ViewController1. I'm using a different class to download the data and another one to save the data. So my question is, how can I update the progress of MBProgressHUD object created in ViewController1 to display the progress to the user?
The approach that I've adopted is to use NSNotificationCenter to send notifications. I'm sending notifications at the end of methods (13) in the class that saves data.
Here's what I've been doing:
//ViewController1.h
#interface ViewController1 ()
{
MBProgressHUD *hud;
float progress;
}
#end
//ViewController1.m
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"downloadComplete"
object:nil];
}
- (IBAction)sumitButtonDidClick:(id)sender {
hud = [[MBProgressHUD alloc] init];
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(#"Please wait...", #"HUD preparing title");
hud.minSize = CGSizeMake(150.f, 100.f);
[hud showAnimated:YES];
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
progress = 0.0f;
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
DownloadClass * obj1 = [[DownloadClass alloc] init];
[obj1 downloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
}
- (void) receiveNotification:(NSNotification *) notification {
if ([[notification name] isEqualToString:#"downloadComplete"])
{
NSLog (#"Successfully received the download complete notification!");
progress += 7.7f;
//[hud setProgress:progress]; // won't work
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD HUDForView:self.view].progress = progress;
});
}
}
Update: I'm receiving the notifications posted by the class that saves
data
You have created a instance variable for MBProgressHUD as MBProgressHUD *hud; and you start progress on a sumitButtonDidClick method as below :
hud = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, -30, 320, 768) ];
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(#"Please wait...", #"HUD preparing title");
hud.minSize = CGSizeMake(150.f, 100.f);
UIWindow *window=[[[UIApplication sharedApplication]delegate]window];
hud.center=window.center;
[window addSubview:hud];
[hud showAnimated:YES];
But increasing progress as Class method of MBProgressHUD as [MBProgressHUD HUDForView:self.view].progress = progress;.
Fix for the issue is below :
dispatch_async(dispatch_get_main_queue(), ^{
hud.progress = progress;
});
if you want to hide the progress you can use
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
According to the documentation for MBProgressHUD you appear to be creating the HUD incorrectly. The example they give, is as follows...
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
// Set the bar determinate mode to show task progress.
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(#"Loading...", #"HUD loading title");
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
You are creating an instance and attaching it to UIWindow
The official documentation lists it like this:
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.label.text = #"Loading";
NSProgress *progress = [self doSomethingInBackgroundCompletion:^{
[hud hideAnimated:YES];
}];
hud.progressObject = progress;
You can find it here: https://github.com/jdg/MBProgressHUD

when Navigate the another page Need to add activity indicator

#import "LoginVC.h"
#import "HOMEVC.h"
#import "DashboardVC.h"
#interface LoginVC ()
#end
#implementation LoginVC
UIActivityIndicatorView *spinner ;
-(IBAction)login:(id)sender{
NSString *userUpdate =[NSString stringWithFormat:#"%#",[Usernamefileld text]];
NSString *userUpdate1 =[NSString stringWithFormat:#"%#",[PasswordField text]];
NSString *baseURL = [NSString stringWithFormat:#"http://192.168.1.200:8094/YazakiService.svc/LOGIN/%#/%#",userUpdate,userUpdate1];
NSURL *url = [NSURL URLWithString:[baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
NSData *responseData =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSMutableArray *serviceResponse=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"got response==%#", serviceResponse);
NSDictionary *template=[serviceResponse objectAtIndex:0];
NSString *test=[template objectForKey:#"ValidState"];
// NSString *test1=[template objectForKey:#"Userid"];
NSString *helloString = #"1";
// //
// NSString *helloString1 =#"LFY430";
if ([test isEqual:helloString]) {
[NSThread detachNewThreadSelector:#selector(threadStartAnimating:) toTarget:self withObject:nil];
[self moveToView];
// UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Login Successfully" message:#"Correct Uername/Password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
else{
UIAlertView *alert2=[[UIAlertView alloc]initWithTitle:#"Login Failed" message:#"Incorrect Uername/Password" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:nil];
[alert2 show];
[self alertView1:alert2 didDismissWithButtonIndex:alert2];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 225, 20, 30)];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
spinner.color = [UIColor blackColor];
[self.view addSubview:spinner];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)moveToView{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DashboardVC *initView = (DashboardVC*)[storyboard instantiateViewControllerWithIdentifier:#"Dashboardvc"];
[initView setModalPresentationStyle:UIModalPresentationFullScreen];
[spinner stopAnimating];
[self presentViewController:initView animated:NO completion:nil];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self moveToView];
}
- (void) alertView1:(UIAlertView *)alertView1 didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LoginVC *initView = (LoginVC*)[storyboard instantiateViewControllerWithIdentifier:#"loginvc"];
[initView setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:initView animated:NO completion:nil];
}
-(void)threadStartAnimating:(id)data
{
[spinner startAnimating];
}
#end
here by i initialse the indicator and start and stop the spinner but its not woking for me...
any one help me to solve the issues how to add the activity indicator when navigation the another view
Thanks in Advance
[self.view addSubview:spinner];
At this line you add the activity indicator to the current ViewController view.
However once you navigate to other view
DashboardVC *initView = (DashboardVC*)[storyboard instantiateViewControllerWithIdentifier:#"Dashboardvc"];
[self presentViewController:initView animated:NO completion:nil];
the current view changes to the new ViewController's view.
The activity indicator isnt present in that view and therefore you wont see your activity indicator there.
To solve, you should again add activity indicator in the second ViewController's viewDidLoad method
In Dashboard VC
- (void)viewDidLoad {
[super viewDidLoad];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 225, 20, 30)];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
spinner.color = [UIColor blackColor];
[self.view addSubview:spinner];
}
A better solution would be to use third party progress huds which make
the loading indicator part absolutely easy like MBProgressHUD
You should add activityindicator on navigation controller so, it is remains on front when you navigate.
you should use MBProgressHud the great third party library.
Just put class in your project and import .h file in your class when you want to show activity indicator and then add HUD (activity indicator) like,
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
// in your case show hud on `self.navigationController.view`
// use main thread to show if required
and hide like,
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Do something...
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view animated:YES];
});
});
Hope this will help :)
When you stop UIActivityIndicatorView then also hide it.
[spinner setHidesWhenStopped:YES];
EDIT:
If you are not sure about NSThread then just do like this.
if ([test isEqual:helloString]) {
//[NSThread detachNewThreadSelector:#selector(threadStartAnimating:) toTarget:self withObject:nil];
[spinner startAnimating];
[self moveToView];
}
And when you want to stop it ten
[spinner stopAnimating];
[spinner setHidesWhenStopped:YES];
instead of using indicator , use svprogresshud.Check this link https://github.com/SVProgressHUD/SVProgressHUD.
if you want animated progressbar check this link https://github.com/cemolcay/GiFHUD

MBProgressHUD is displayed only when the operation is finished

i'm using MBProgressHUD to show a popup with a loading animation but i have a problem. The progress indicator call is executed when i press a button. This is the action
- (IBAction)calendarioButtonPressed:(UIButton *)sender {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = #"Uploading";
[hud show:YES];
[self getCalendarioJson];
}
in the getCalendarioJson i have this
- (void) getCalendarioJson {
//LETTURA CALENDARIO - INIZIO
NSString *calLink = myLink;
NSData* responseData = [NSData dataWithContentsOfURL:[NSURL URLWithString:calLink]];
NSArray* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions error:nil];
NSDictionary *firstObject = [json objectAtIndex:0];
NSDictionary *cal = [firstObject objectForKey:#"output"];
variabiliGlobali.calendario = [[NSMutableArray alloc] init];
for (NSDictionary *dict in cal) {
[variabiliGlobali.calendario addObject: dict];
}
//LETTURA CALENDARIO - FINE
}
Why does the loading popup appear only at the end of getCalendarioJson execution?
The button have a segue. When i go back from the target view i can see the popup.
What is the metter? If i delete the segue i can see the popup at the end of getCalendarioJson execution (because there is not a segue).
Thanks in advance.
You have a method [self getCalendarioJson]; but this method run in the main thread, the main thread is for UI updates, when you download data or make a operation with several seconds of duration, the main thread wait for make the UI update.
For solve this situation, put the method in a background thread, can you use [self performSelectorInBackground:#selector(getCalendarioJson) withObject:nil];
From https://github.com/jdg/MBProgressHUD
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText = #"Loading";
[self doSomethingInBackgroundWithProgressCallback:^(float progress) {
hud.progress = progress; //Here you set the progress
} completionCallback:^{
[hud hide:YES];
}];
You should set the progress value.

MBProgressHUD gracetime and minimumtime not working properly. how to implement.?

I am using MBProgressHUD for showing loading while performing local long method to put user good experience. All working fine but now i have to implement like that if the method executes within a second than the loading should not be appear. I have gone through the MBProgressHUD samples and for this functionality i found the setGraceTime and setMinShowTime show time but its all not working properly. because when i put set grace time the loading icon is not appear even the method execution time is more than 1 second.Here is my code
if (self.HUD == nil)
{
self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:self.HUD];
}
self.HUD.labelText = #"Please wait.....";
// [self.HUD setMinShowTime:2.0f];
[self.HUD setGraceTime:1.0f];
[self.HUD setTaskInProgress:YES];
[self.HUD show:YES];
// [self.HUD hide:YES afterDelay:3];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];
if (mycondition == nil)
{
[self myTask:[NSURL fileURLWithPath:strPath]];
//[self performSelector:#selector(mytask:) withObject:[NSURL fileURLWithPath:strPath]];
}
else
{
//else
}
self.tempView.hidden = YES;
// self.HUD.taskInProgress = NO;
[self.HUD hide:YES];
self.HUD = nil;
I have putted [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; for this problem's solution
please guide me what wrong in this code..?!
You need to remove the MBProgressHUD object from its superview in the method execution.
Like this:
// if the method runs inside one second, the indicator will not be
shown
- (void)viewDidLoad
{
[super viewDidLoad];
if (HUD == nil)
{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
}
HUD.labelText = #"Please wait.....";
[HUD setGraceTime:1.0f];
[HUD setTaskInProgress:YES];
[HUD show:YES];
[self performSelector:#selector(runThisMethod) withObject:nil afterDelay:0.9f];
}
- (void)runThisMethod
{
[HUD removeFromSuperview];
}
// if the method runs after one second, the indicator will be
shown for some time until the method runs
- (void)viewDidLoad
{
[super viewDidLoad];
if (HUD == nil)
{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
}
HUD.labelText = #"Please wait.....";
[HUD setGraceTime:1.0f];
[HUD setTaskInProgress:YES];
[HUD show:YES];
[self performSelector:#selector(runThisMethod) withObject:nil afterDelay:1.9f];
}
- (void)runThisMethod
{
[HUD removeFromSuperview];
}

MBProgressHUD not resetting

I'm using MBProgressHUD in our teaching App, which is tab bar navigated.
The user will come from a tableview via Urban Airship's storefront directly to the UA detail view.
Once buy is clicked I bring on HUD with
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[self.view.window addSubview:HUD];
It is using the showWhileExecuting statement.
It goes through three while statements to change from "Connecting" to "Downloading" to "Unpacking".
All working perfectly OK.
Here comes the problem...
The second time I do this the label text will not change. It is stuck on "Connecting".
I can see in the NSLog that it is going through the other loops.
On top of that, if I try to change the Mode, the app crashes.
This only happens the second time, and any subsequent uses. If I kill the App everything works again for the first time.
Looks to me that MBProgressHUD doesn't get reset when it's finished.
(ARC is used in the project)
Anyone with a solution?
Thanks
Edit:
- (void)showWithLabelDeterminate
{
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
HUD.mode = MBProgressHUDModeIndeterminate;
[self.view.window addSubview:HUD];
HUD.delegate = self;
HUD.labelText = NSLocalizedString(#"Connecting","");
HUD.detailsLabelText = #" ";
HUD.minSize = CGSizeMake(145.f, 145.f);
HUD.dimBackground = YES;
[HUD showWhileExecuting:#selector(lessonDownloadProgress) onTarget:self withObject:nil animated:YES];
}
-(void)lessonDownloadProgress
{
DataManager *sharedManager = [DataManager sharedManager];
// HUD.mode = MBProgressHUDModeIndeterminate;
HUD.labelText = nil;
HUD.detailsLabelText = nil;
while ([sharedManager.downHUD floatValue] == 0.0f)
{
[self parentViewController];
NSLog(#"HUD lessonDownloadProgress: %f", HUD.progress);
HUD.labelText = NSLocalizedString(#"Connecting","");
HUD.detailsLabelText = #" ";
NSLog(#"Waiting for download to start");
// Wait for download to start
usleep(80000);
}
// Switch to determinate mode
// HUD.mode = MBProgressHUDModeDeterminate;
HUD.labelText = NSLocalizedString(#"DownLoading","");
HUD.progress = [sharedManager.downHUD floatValue];
while (HUD.progress < 1.0f && [sharedManager.cleanedUp isEqualToString:#"No"])
{
// [self parentViewController];
HUD.labelText = NSLocalizedString(#"Downloading","");
NSLog(#"HUD lessonDownloadProgress: %f", HUD.progress);
HUD.progress = [sharedManager.downHUD floatValue];
NSString *percent = [NSString stringWithFormat:#"%.0f", HUD.progress/1*100];
HUD.detailsLabelText = [percent stringByAppendingString:#"%"];
usleep(50000);
}
// Switch HUD while cleanUp
HUD.mode = MBProgressHUDModeIndeterminate;
while ([sharedManager.cleanedUp isEqualToString:#"No"])
{
[self parentViewController];
HUD.labelText = NSLocalizedString(#"Unpacking","");
HUD.detailsLabelText = #" ";
// wait for cleanup
NSLog(#"Waiting for clean up");
usleep(50000);
}
NSLog(#"++ Finished loops ++");
NSLog(#"Finished HUD lessonDownloadProgress: %f", HUD.progress);
[MBProgressHUD hideHUDForView:self.view animated:YES];
[HUD removeFromSuperview];
HUD.delegate = nil;
[HUD release];
HUD = nil;
}
I cannot spot the issue in the code you posted; but some refactoring might help.
Rather than polling the DataManager you could use KVO to observe properties on the DataManager and respond to those changes. ("Don't call us; we'll call you.) So here's a suggested approach if you want.
Your class interface:
#interface YourClass : UIViewController // or whatever your superclass is...
{
MBProgressHUD *_hud;
DataManager *_dataManager;
// your other ivars
}
#end
And in your implementation file...
#interface YourClass()
#property (nonatomic, retain) DataManager dataManager;
#end
Above I've declared your dataManager as a property so that we can observe it.
To start the download process we now have a method downloadLesson:
- (void)downloadLesson;
{
// show HUD and retain it (showHUDAddedTo:animated: returns autoreleased object)
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
// observe properties on the dataManager
[self addObserver:self forKeyPath:#"dataManager.progress" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:#"dataManager.cleanedUp" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:#"dataManager.downHUD" options:NSKeyValueObservingOptionNew context:nil];
// begin your download here...
HUD.labelText = NSLocalizedString(#"Connecting", "");
HUD.detailsLabelText = #" ";
HUD.progress = self.dataManager.downHUD;
}
Now use KVO to update the appearance of the HUD:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
if( [keyPath isEqualToString:#"dataManager.cleanedUp"] )
{
if( [[[self dataManager] cleanedUp] isEqualToString:#"Yes"] )
{
[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES];
[HUD release]; HUD = nil;
[self removeObserver:self forKeyPath:#"dataManager.progress"];
[self removeObserver:self forKeyPath:#"dataManager.cleanedUp"];
[self removeObserver:self forKeyPath:#"dataManager.downHUD"];
}
}
if( [keyPath isEqualToString:#"dataManager.downHUD"] )
{
// if the data manager updates progress, update our HUD
HUD.progress = self.dataManager.downHUD;
if( self.dataManager.downHUD == 0.0 )
// no progress; we're just connecting
HUD.labelText = NSLocalizedString(#"Connecting", "");
else if( self.dataManager.downHUD < 1.0 )
{
// progress >0.0 and < 1.0; we're downloading
HUD.labelText = NSLocalizedString(#"Downloading", "");
NSString *percent = [NSString stringWithFormat:#"%.0f%%", HUD.progress/1*100];
HUD.detailsLabelText = percent;
}
else
{
// progress == 1.0, but we haven't cleaned up, so unpacking
if( [[[self dataManager] cleanedUp] isEqualToString:#"No"] )
{
HUD.labelText = NSLocalizedString(#"Unpacking","");
HUD.detailLabelsText = #" ";
}
}
}
}
Alternatively, you could use notifications to do the updates, wherein the DataManager posts NSNotifications for which your view controller is registered. Or, if you were open to refactoring the DataManager you could use blocks to do the updates. All of these solutions avoid having to explicitly block your thread to poll the DataManager. Hope this helps.
Did you implement the delegate method?
- (void)hudWasHidden:(MBProgressHUD *)hud {
// Remove HUD from screen when the HUD was hidded
[HUD removeFromSuperview];
HUD = nil;
}
This is because the label is set up on init.Try this:
You should just add this method to the header file of MBProgressHud:
+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view withText:(NSString *)text;
And implement it in the .m file as follows:
+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view withText:(NSString *)text
{
MBProgressHUD *hud = [[self alloc] initWithView:view];
hud.labelText = text;
[view addSubview:hud];
[hud show:YES];
return MB_AUTORELEASE(hud);
}
and call it wherever you want like:
[MBProgressHUD showHUDAddedTo:self.view withText:#"Loading..."];

Resources