how to add text while displaying MBProgressHud in iOS? - ios

I want to add text while displaying progress hud.
But i don't know how to use it.
Please help me out for this.
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
[hud hideAnimated:YES];

#import "MBProgressHUD.h"
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:markerView animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = #"Loading";
hud.detailsLabel.text = #"Wait 3 seconds";
//hide MBProgressHUD when process finish
[hud hideAnimated:YES];
download from this link: https://github.com/jdg/MBProgressHUD

u can set labeltext
hud.labelText = #"Loading";
for details
hud.detailsLabelText = #"Details";

Related

Showing toast message with MBProgressHUD in mac os

I am showing toast message using MBProgressHUD but with toast message getting some view with it Below is code is using
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window.contentView animated:YES];;
// Configure for text only and offset down
hud.mode = MBProgressHUDModeText;
hud.labelText = #"some message......";
hud.margin = 10.f;
hud.yOffset = 200.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:20];
For macOS Using MBProgressHud you can show like this
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:window.contentView];
hud.mode = MBProgressHUDModeText;
hud.labelFont = [NSFont systemFontOfSize:13.0];
hud.margin = 8.f;
hud.opacity = 0.7;
hud.yOffset = NSHeight(window.frame)/2-60;
hud.cornerRadius = 4.0;
hud.removeFromSuperViewOnHide = YES;
hud.detailsLabelText = #"some message......";
[window.contentView addSubview:hud];
[hud show:YES];
[hud hide:YES afterDelay:2.0];
Code:
- (void)showLoader:(NSString*)strTitle view:(UIView*)view
{
dispatch_async(dispatch_get_main_queue() , ^{
MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view.window];
[view.window addSubview:hud];
hud.labelText = strTitle;
[hud show:YES];
});
}
Use like this:
[self showLoader:#"Loading.." view:self.view]
//JUST REPLACE YOUR CODE AND CHECK
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:appDel.window animated:YES];

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

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.

How to add MBProgressHUD waiting loading data in iOS

I have a problem: I used AFNetworking to get data from server, i used NSOperationQueue to add many operation to it, in each request, I added this operation to queue and used waitUntilAllOperationsAreFinished as bellow :
request 1
...
[queue addOperation:operation1];
[queue waitUntilAllOperationsAreFinished];
request 2
...
[queue addOperation:operation2];
[queue waitUntilAllOperationsAreFinished];
I tried above code and my programs seems hangs and after that, it runs ok.So that I want to added MBProgressHUD to waiting queue finish, then I want to check if queue finish, I want to hide MBProgressHUD. But when i click on Button to load UIViewController, MBProgressHUD cannot show.
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Loading";
Actually, I want to show MBProgressHUD when queue finish. How can i do that? Thanks all
Shortly you can do it like this:
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
[MBProgressHUD hideHUDForView:self.view animated:YES];
Check MBProgressHUD's usage
Another Better Approach..
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.labelText = #"Doing funky stuff...";
HUD.detailsLabelText = #"Just relax";
HUD.mode = MBProgressHUDModeAnnularDeterminate;
[self.view addSubview:HUD];
[HUD showWhileExecuting:#selector(doSomeFunkyStuff) onTarget:self withObject:nil animated:YES];
And doSomeFunkyStuff
- (void)doSomeFunkyStuff {
float progress = 0.0;
while (progress < 1.0) {
progress += 0.01;
HUD.progress = progress;
usleep(50000);
}
}
Detail answer is here..
waitUntilAllOperationsAreFinished will bock the current thread, which is probably the main thread so you really don't want to do that.
If you're using AFNetworking then check out this method on AFHTTPClient
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
So show you HUD then call this method and hide your HUD in the completionBlock

MBProgressHUD indicator does not hide

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";
}

Resources